It is necessary to compare 2 numbers in one numeral system, numbers should be entered as strings, possible systems from 2 to 16

Translation to int

int CharToInt(char *s) { int result = 0; result = (int)s - (int)'0'; if( result<0 || result>9){ // для 16 системи обработка А-F result = (int)s - (int)'0' - 7; } return result; } 

Translation into 10 system for comparison

 int ToDec(char *number,int systemNumber){ int result = 0; //результат int length = strlen(number),// к-во чисел в строке i, //счетчик цикла c_str=0; //счетчик для строки int step = length-1; //степень в какую возводим for(i=0;i<length;i++){ result += CharToInt(number[c_str]) * pow(systemNumber,step);//1 *10^2+0*10^1+0*10^0 step--; // уменьшаем степень c_str++; //next цифра } return result; } 

For ToDec("100",10) values ToDec("100",10) for some reason, it returns 99 instead of 100

  • It's time to take in hand debugger. - αλεχολυτ

1 answer 1

Corrected a compilation error. You need to change the signature of the CharToInt function to

 int CharToInt(char s) 

Before this, the pointer to char was taken as a C-style string, not a character.

 Result: 100. 
  • Strange, I still return 99, the pointer is removed. - Hardc0re
  • Yes, I went through the debugger - everything works fine. Try also debugger to see the correctness of the work - Malov Vladimir
  • Compiled in VS 2013 - everything works as it should - 100. In CodeBlocks returns 99. As I understand it in the compiler. - Hardc0re
  • Very strange. In CodeBlocks, you can trace the code? See why exactly this value turned out - Malov Vladimir