Преобразование, которое я написал от шестнадцатеричного до десятичного числа, не работает. Я не уверен, какая часть ошибочна. Нужно ли мне делать -7 или что-то еще.Десятичная до шестнадцатеричной ошибки преобразования
int hex_to_dec(char hexnumber[])
{
int decimal = 0; //integer for the final decimal number
int bit; //integer representing numbers between 0-9 and letter a-f in hex number
//a char array containing the input hex number
int i=0,j=0;
//the integer i takes the length of the input array
i =strlen(hexnumber);
//while there is a next bit in the array
while(i!=0)
{
bit = hexnumber[j];
//if the bit is a digit do the following
if(('0' <= bit && bit <= '9'))
{
decimal = decimal * 16;
decimal = decimal + (bit - '0');
}
//if the bit is a letter do the following
if(('a' <= bit && bit <= 'z'))
{
decimal = decimal * 16;
decimal = decimal + (bit - '7');
}
i--;
j++;
}
if(('a' <= bit && bit <= 'z'))
{
decimal = decimal * 16;
decimal = decimal + (bit - '7');
}
cout<<decimal;
return decimal;
}
Вышеупомянутый мой код для того же.
Используйте отладчик. Напишите тест (ы). –
Используйте стандартные функции, такие как 'isdigit' и' isalpha' вместо того, чтобы кататься самостоятельно. – chris
Трюк «value-7» работает для «A..F» * после * вычитания «0» - по крайней мере в ASCII. Это говорит о том, что вы скопировали части своего кода из другого места - только не правильно. – usr2564301