但要注意的是轉出來數值的範圍
資料型態
|
最大值(Decimal)
|
最大值(Hexadecimal)
|
unsigned
long
|
4,294,967,295
|
0xFFFFFFFF
|
unsigned
long long
|
18,446,744,073,709,551,615
|
0x1999999999999999
|
如表格所示,如果hex string數值超過0xFFFFFFFF,就要用stroull()來轉換。
程式碼
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char hex_value1[]="0xa84a5011";
char hex_value2[]="0xa84a5011cdef";
unsigned long result1 = strtoul(hex_value1, NULL, 0);
unsigned long result2 = strtoul(hex_value2, NULL, 0);
unsigned long long result3 = strtoull(hex_value2, NULL, 0);
printf("[%s]: 0x%lx\n", hex_value1, result1);
printf("[%s]: 0x%lx\n", hex_value2, result2);
printf("[%s]: 0x%llx\n", hex_value2, result3);
return 0;
}
測試結果
沒有留言:
張貼留言