我写了一个代码将ip转换为十进制,它似乎给出了意想不到的结果,这是因为从BYTE到DWORD的转换不匹配。BYTE转换为DWORD
有没有办法将字节变量转换为字,类型转换似乎不起作用。
下面是代码
//function to convert ip 2 decimal
DWORD ip2dec(DWORD a ,DWORD b,DWORD c,DWORD d)
{
DWORD dec;
a=a*16777216;
b=b*65536;
c=c*256;
dec=a+b+c+d;
return dec;
}
int main()
{
BYTE a,b,c,d;
/* some operations to split the octets and store them in a,b,c,d */
DWORD res=ip2dec(a,b,c,d);
printf("The converted decimal value = %d",dec);
}
我得到的价值为-1062731519代替3232235777的部分。
首先发布你的实际代码,因为你在这个错误,例如什么是在printf dec?第二你可能想在你的printf中使用“%u” –