Browse Source

defined and implemented function to convert hex input to decimal

remotes/origin/conversionOfNumbers
Laurin 11 months ago
parent
commit
ded7c60fcb
  1. 20
      src/conversionOfNumbers/conversionOfNumbers.c
  2. 1
      src/conversionOfNumbers/conversionOfNumbers.h

20
src/conversionOfNumbers/conversionOfNumbers.c

@ -87,3 +87,23 @@ char* convertIntToHex(int input){
}while(input != 0);
return result;
}
unsigned int convertHexStrToInt(char* input){
unsigned int result = 0;
int index = strlen(input) - 1;
int exponent = 0;
while(index >= 0){
switch(input[index]){
case 'A': case 'a': result += 10 * pow(16, exponent); break;
case 'B': case 'b': result += 11 * pow(16, exponent); break;
case 'C': case 'c': result += 12 * pow(16, exponent); break;
case 'D': case 'd': result += 13 * pow(16, exponent); break;
case 'E': case 'e': result += 14 * pow(16, exponent); break;
case 'F': case 'f': result += 15 * pow(16, exponent); break;
default: result += (input[index] - '0') * pow(16, exponent); break;
}
index--;
exponent++;
}
return result;
}

1
src/conversionOfNumbers/conversionOfNumbers.h

@ -6,5 +6,6 @@ int binaryStrLen(int input);
unsigned int convertBinaryStrToInt(char* input);
char* convertIntToHex(int input);
int hexStrLen(const int input);
unsigned int convertHexStrToInt(char* input);
#endif
Loading…
Cancel
Save