gpt4 book ai didi

c++ - 十进制转十六进制错误

转载 作者:行者123 更新时间:2023-11-28 00:38:03 24 4
gpt4 key购买 nike

<分区>

我写的十六进制到十进制数的转换不起作用。我不确定哪一部分是错的。我是否必须执行 -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;
}

上面是我的代码。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com