I tried to use writeStr to print out if the Hex converted to a decimal but all I get is the input i passed thru strToInt.
for example i chose A and it prints out an A but i wanted to print out a 10
Code:#include <stdio.h> #include "readStr.h" #include "writeStr.h" #include "strToInt.h" #define BUFFER_SIZE 33 #define HEX_SIZE 9 #define BIN_SIZE 33 #define DEC_SIZE 11 #define BASE_SIZE 2 int main() { char input[BUFFER_SIZE]; char base[2]; unsigned int x; writeStr("\nEnter the base of the value to be converted"); writeStr("\n d-decimal, h-hexadecimal, b-binary: "); readStr(base, BASE_SIZE); if(base[0] =='d' || base[0] =='D') { writeStr("Enter the Decimal Value: "); readStr(input, 11); } else if(base[0] =='h' || base[0] =='H') { writeStr("Enter the Hex Value: "); readStr(input, 9); } else if(base[0] =='b' || base[0] =='B') { writeStr("Enter the Binary Value: "); readStr(input, 33); } else {writeStr("Invalid Input - Goodbye");} strToInt(input, base[0]); writeStr(input); return 0; }
Code:int strToInt(char *string, int base) { int decimal_val; int result = 0; int i; if(base == 'h' || base =='H') { /* Goes through each value of string*/ for ( i = 0 ; string[i] != '\0' ; i++ ) { /* The conversion */ decimal_val = '0' <= string[i] && string[i] <= '9' ? string[i] - '0' : 'a' <= string[i] && string[i] <= 'f' ? string[i]- 'a' + 10 : 'A' <= string[i] && string[i] <= 'F' ? string[i]- 'A' + 10 : 0; result = result * 16 + decimal_val; } } return result; }



LinkBack URL
About LinkBacks


