如何从十六进制字符串中获取十进制数:
我有一个unsigned char* hexBuffer = "eb89f0a36e463d";
还有一个unsigned char* hex[5] ={'\\','x'};
我将hexBuffer
的前两个字符"eb"
复制到hex[2] = 'e'; hex[3] = 'b';
.
现在在hex中有字符串"\xeb"
或"\xEB"
.
我们都知道0xEB
是十六进制,可以转换为235
十进制。
如何将"\xEB"
转换为235(int)
?
(感谢jedwards)
我的答案(也许对某些人有用):
/*only for lower case & digits*/
unsigned char hash[57] ="e1b026972ba2c787780a243e0a80ec8299e14d9d92b3ce24358b1f04";
unsigned char chr =0;
int dec[28] ={0}; int i = 0;int c =0;
while( *hash )
{
c++;
(*hash >= 0x30 && *hash <= 0x39) ? ( chr = *hash - 0x30) : ( chr = *hash - 0x61 + 10);
*hash++;
if ( c == 1) dec[i] = chr * 16; else{ dec[i] += chr; c = 0; dec[i++];}
}
E * 16^1 + B * 16^0
。请注意,没有幂运算符。 - chris0xE
而不是'E'
,Chris 也是这样。 - OmnipotentEntity