Advertisement
| Hall of Fame |
|
[x]
Posted via EE Mobile
|
||
Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again. |
||
| Question |
|
[x]
Attachment Details
|
||
|
[x]
The Solution Rating System
|
||
With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.
Your Input Matters If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support. Thank you! |
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: |
#include <iostream>
#include <stdio.h>
#include <string.h>
int length_of_line ( char []);
int convert_binary (char [], int);
int convert_to_decimal(char[],int);
char convert_binary_to_hex(char [] );
int convert_to_binary(int );
int main ()
{
char line [100];//character string
printf("\t1.Enter a binary number (or press 99 to enter a non-binary number)>\n", line);
scanf("%s", &line);
int n;
if (!strncmp(line, "99", 2))
{
printf("Please enter a number to convert to binary>\n");
scanf("%s", &line);
do
{
printf("What is the base of the number?");
scanf("%d", &n);
// printf("%s%d%s%d\n", "base ", n, " value is ", ans);
}
while ( n!= 8 && n!= 10 && n!= 16);
int ans = convert_to_decimal (line, n);
int binary_ans = convert_to_binary (ans);
//printf("the answer is %d", binary_ans);
}
else
{
do
{
printf("What is the base do you want to convert the binary number to?");
scanf("%d", &n);
}
while ( n!= 8 && n!= 10 && n!= 16);
int ans = 0;
char ans_hex = NULL;
char copy[4];
int length = strlen(line);
int endingCondition = 0;
int i =0;
if (n == 16) {
printf("%s\n", "Converted to hex: ");
while (endingCondition < length) {
for (i=0; i<4; i++ ) {
copy[i] = line[i+endingCondition];
}
ans_hex = convert_binary_to_hex (copy);//sends in four digit array
printf("%c", ans_hex);
endingCondition += 4;
}
} else {
ans = convert_binary (line, n);
}
//printf("%s%d%s%d\n", copy, n, " value is ", ans);
}
}
//function to convert base 8, 10, and 16 to decimal
int convert_to_decimal (char value [], int n) {
int length = strlen(value);
int power = 1;
int sum = 0;
int i=0;
if (n == 8) {
int charValue=0;
for (i=length-1; i>=0; --i)//start at end of string
{
//if value[i] is not a 1 or 0 then its not a valid binary number
if (value[i] < 48 ||value[i] > 54) {
printf("%s\n", "invalid octal input now exiting");
exit(1);
}
//convert the ascii value of the character to decimal so we can evaluate it
charValue = value[i] -48;
if (charValue != 0) {
sum = sum + ( charValue * power);
}
power = power * 8;
}
return sum;
}
else if (n == 10) {
int charValue=0;
for (i=length-1; i>=0; --i)//start at end of string
{
//if value[i] is not a 1 or 0 then its not a valid binary number
if (value[i] < 48 ||value[i] > 57) {
printf("%s\n", "invalid decimal input now exiting");
exit(1);
}
//convert the ascii value of the character to decimal so we can evaluate it
charValue = value[i] -48;
if (charValue != 0) {
sum = sum + ( charValue * power);
}
power = power * 10;
}
return sum;
}
else {
int charValue=0;
for (i=length-1; i>=0; --i)//start at end of string
{
//if value[i] is not a 1 or 0 then its not a valid binary number
if (!(value[i] >= 48 && value[i] <= 57) && !(value[i] >= 65 && value[i] <= 70) && !(value[i] >= 97 && value[i] <= 102)){
printf("%s\n", "invalid hex input now exiting");
exit(1);
}
//convert the ascii value of the character to decimal so we can evaluate it
if (value[i] >= 48 && value[i] <= 57)
{
charValue = value[i] -48;
}
else if (value[i] >= 65 && value[i] <= 70)
{
charValue = value[i] -55;
}
else
{
charValue = value[i] -87;
}
if (charValue != 0) {
sum = sum + ( charValue * power);
}
power = power * 16;
}
return sum;
}
}
//function to convert decimal number to binary
int convert_to_binary(int m)
{
int rem;
int store [100];
int i = 0;
int j;
do
{
rem = m%2;
m = m/2;
store[i] = rem;
++i;
}
while (m!=0);
for (j=(i-1); j>=0; --j){
printf("%d", store[j]);
}
}
//function to convert binary number to base 8 and base 10
int convert_binary (char value [], int n)
{
int length = strlen(value);
int power = 1;
int sum = 0;
int i=0;
int hold=0;
int ten = 1;
if (n == 8)
{
int charValue=0;
int count=0;
for (i=length-1; i>=0; --i)
{
//if value[i] is not a 1 or 0 then its not a valid binary number
if (value[i] != 48 && value[i] != 49) {
printf("%s\n", "invalid binary input now exiting");
exit(1);
}
//convert the ascii value of the character to decimal so we can evaluate it
charValue = value[i] -48;
if (charValue != 0) {
sum = sum + ( charValue * power);
}
power = power * 2;
++count;
if (count == 3)
{
count = 0;
hold = hold + (sum * ten);
ten = ten * 10;
sum = 0;
power = 1;
}
}
hold = hold+(ten*sum);
return hold;
}
else if (n == 10)
{
int charValue=0;
for (i=length-1; i>=0; --i)
{
//if value[i] is not a 1 or 0 then its not a valid binary number
if (value[i] != 48 && value[i] != 49) {
printf("%s\n", "invalid binary input now exiting");
exit(1);
}
//convert the ascii value of the character to decimal so we can evaluate it
charValue = value[i] -48;
if (charValue != 0) {
sum = sum + ( charValue * power);
}
power = power * 2;
}
return sum;
}
return 1;
}
//Function to convert binary number to hexadeciaml
char convert_binary_to_hex(char line[]) {
int count =0;
int length = strlen(line);
for (int i = 0; i < 4; i++ ) {
if (line[i] == 48 || line[i] == 49 ) {
count++;
}
}
switch (count){
case 4:
if (!strncmp(line, "0000", 4)){
return '0';
}else if (!strncmp(line, "0001", 4)) {
return '1';
}else if (!strncmp(line, "0010", 4)) {
return '2';
}else if (!strncmp(line, "0011", 4)) {
return '3';
}else if (!strncmp(line, "0100", 4)) {
return '4';
}else if (!strncmp(line, "0101", 4)) {
return '5';
}else if (!strncmp(line, "0110", 4)) {
return '6';
}else if (!strncmp(line, "0111", 4)) {
return '7';
}else if (!strncmp(line, "1000", 4)) {
return '8';
}else if (!strncmp(line, "1001", 4)) {
return '9';
}else if (!strncmp(line, "1010", 4)) {
return 'A';
}else if (!strncmp(line, "1011", 4)) {
return 'B';
}else if (!strncmp(line, "1100", 4)) {
return 'C';
}else if (!strncmp(line, "1101", 4)) {
return 'D';
}else if (!strncmp(line, "1110", 4)) {
return 'E';
}else if (!strncmp(line, "1111", 4)) {
return 'F';
}
break;
case 3:
if (!strncmp(line, "000", 3)){
return '0';
} else if (!strncmp(line,"001", 3)) {
return '1';
}else if (!strncmp(line, "010", 3)) {
return '2';
}else if (!strncmp(line, "011", 3)) {
return '3';
}else if (!strncmp(line, "100", 3)) {
return '4';
}else if (!strncmp(line, "101", 3)) {
return '5';
}else if (!strncmp(line, "110", 3)) {
return '6';
}else if (!strncmp(line, "111", 3)) {
return '7';
}
break;
case 2:
if (!strncmp(line, "00", 2)){
return '0';
} else if (!strncmp(line, "01", 2)) {
return '1';
}else if (!strncmp(line, "10", 2)) {
return '2';
}else if (!strncmp(line, "11", 2)) {
return '3';
}
break;
case 1:
if (!strncmp(line, "0", 1)){
return '0';
} else if (!strncmp(line, "1", 1)) {
return '1';
}
break;
default:
printf("Error\n");
break;
}
}
|