C语言中如何把字符串"2A"转换成0x2A?

如题:C语言中如何把字符串"2A"转换成0x2A?
有没有现成的函数,或者提供一直转换思路?

思路:对字符串中每个字符判断是 0 到 9,还是 A 到 F
对这两种情况进行处理,是第一种减去 0x30 就是对应的数字;第二种减去 'A',再加上 10。如果字母有小写的情况,也需要考虑。

atoi 得到数字,十六进制就是想要的结果

搞错了,还回了两次,真是晕啊!

没有现成的函数,自己写吧,很简单。

思路,手写的。
int result = 0;
for (int i = 0; i < strlen(s); i++)
{
result += (s[i] < '0' ? s[i] - 'A' + 10 : s[i] - '0') << (strlen(s) - 1 - i) * 4;
}

/*
return 16 error!
/
unsigned char hexchar2hex(char hexchar){
switch(hexchar){
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'A': return 10;
case 'a': return 10;
case 'B': return 11;
case 'b': return 11;
case 'C': return 12;
case 'c': return 12;
case 'D': return 13;
case 'd': return 13;
case 'E': return 14;
case 'e': return 14;
case 'F': return 15;
case 'f': return 15;
default: return 16;
}
}
/

return 16 error!
*/
unsigned char hexchar2hex(char hexchar){
if(isdigit(hexchar)){
return hexchar - '0';
}
if(islower(hexchar) && hexchar < 'g'){
return hexchar - 'a' + 10;
}
if(isupper(hexchar) && hexchar < 'G'){
return hexchar - 'A' + 10;
}
return 16;
}

/*
return -1 error!
return 1 succ
*/
int hexstr2hex(char *hexstrp, char **ret_hex, int *ret_len){
int len;
int hex_low;

if(hexstrp == NULL || hexstrp[0] == '\0' || *ret_hex != NULL || ret_len == NULL){
    return -1;
}

len = strlen(hexstrp);
*ret_len = (len-1)/2 + 1;
*ret_hex = (char *)malloc(*ret_len);
if(*ret_hex == NULL){
    return -1;
}
memset(*ret_hex, 0, *ret_len);
for(i = 0; i < len; i++){
    if((hex_low = hexchar2hex(hexstrp[len - 1 - i])) == 16){
        free(*ret_hex);
        *ret_hex = NULL;
        return -1;
    }
    (*ret_hex)[i/2] = hex_low;
    i++;
    if(i < len){
        if(hex_low = hexchar2hex(hexstrp[len - 1 - i])) == 16){
            free(*ret_hex);
            *ret_hex = NULL;
            return -1;
        }
        (*ret_hex)[i/2] = (*ret_hex)[i/2] + (hex_low * 16);
    }
}
return 1;

}

#include
#include
#include

/*
return 16 error!
*/
unsigned char hexchar2hex(char hexchar){
if(isdigit(hexchar)){
return hexchar - '0';
}
if(islower(hexchar) && hexchar < 'g'){
return hexchar - 'a' + 10;
}
if(isupper(hexchar) && hexchar < 'G'){
return hexchar - 'A' + 10;
}
return 16;
}

/*
return -1 error!
return 1 succ
*/
int hexstr2hex(char *hexstrp, char **ret_hex, int *ret_len){
int len;
int hex_low;
int i;

if(hexstrp == NULL || hexstrp[0] == '\0' || *ret_hex != NULL || ret_len == NULL){
    return -1;
}

len = strlen(hexstrp);
*ret_len = (len-1)/2 + 1;
*ret_hex = (char *)malloc(*ret_len);
if(*ret_hex == NULL){
    return -1;
}
memset(*ret_hex, 0, *ret_len);
for(i = 0; i < len; i++){
    if((hex_low = hexchar2hex(hexstrp[len - 1 - i])) == 16){
        free(*ret_hex);
        *ret_hex = NULL;
        return -1;
    }
    (*ret_hex)[*ret_len - 1 - i/2] = hex_low;
    i++;
    if(i < len){
        if((hex_low = hexchar2hex(hexstrp[len - 1 - i])) == 16){
            free(*ret_hex);
            *ret_hex = NULL;
            return -1;
        }
        (*ret_hex)[*ret_len - 1 - i/2] = (*ret_hex)[*ret_len - 1 - i/2] + (hex_low * 16);
    }
}
return 1;

}

int main(int argc, char **argv){
char *a = "b32ac";
char *b = NULL;
int len = NULL;
int ret;
int i;

printf("hexstr2hex return value is: %d\r\n", a, ret = hexstr2hex(a, &b, &len));
if(ret == 1){
    printf("%s = ", a);
    for(i = 0; i < len; i++){
        printf("%02x", (unsigned char)(b[i]));
    }
}
free(b);
b = NULL;
return 0;

}

man page里面来的,应该是最容易的方法
const char str = "2A";
int i = (int)strtol(str, (char *
)NULL, 16);

#define uint8_t unsigned char
#define BUILD_UINT8(loByte, hiByte ) \
    ((uint8_t)(((loByte) & 0x0F) + (((hiByte) & 0x0F) << 4)))

int main(){
    char ID1[12] = "012a3b4c5b6d";
    uint8_t mesh_id[6];
    uint8_t temp_id[12];
    int i;

    for(i = 0; i < 12 ; i++) {
        if(ID1[i] >= '0' && ID1[i] <= '9') {
            temp_id[i] = ID1[i] - '0';
        } else {
            temp_id[i] = ID1[i] - 'a' + 10;
        }
        printf(" %d = %d  ", i, temp_id[i]);
    }

    for(i = 0; i < 12 ;) {
        mesh_id[i/2] = BUILD_UINT8(temp_id[i+1], temp_id[i]);
        i += 2;
    }

    for(i = 0; i < 6 ; i++) {
        printf("%#2x\n", mesh_id[i]);
    }
    return 0;
}