本文摘自:http://www.chineselinuxuniversity.net/articles/40274.shtml
方法1: sscanf()
函數名: sscanf
功 能: 從字符串格式化輸入
用 法: int sscanf(char *string, char *format[,argument,...]);
以上的 format 爲 %x 就是將字符串格式化爲 16 進制數
例子:
#include <stdio.h> void main() { char* p = "0x1a"; int nValude = 0; sscanf(p, "%x", &nValude); printf("%d\r\n", nValude); }
輸出:
26
方法2: strtol()
函數名: strtol
功 能: 將字符串轉換爲長整數
用 法: long strtol(char *str, char **endptr, int base);
上面的base就是我們要轉換爲幾進制數
例子:
#include <stdio.h>
#include <stdlib.h>
void main()
{
char* p = "0x1b";
char* str;
long i = strtol(p, &str, 16);
printf("%d\r\n", i);
}
輸出:
27