如何将十六进制字符串(char [])转换为整数?

105

我有一个char[]类型的值,例如 "0x1800785",但是我想将其赋值给需要int类型的函数,如何将其转换为int类型?我搜索了一下但找不到答案。谢谢。


1
可能是将字符串转换为有符号整数的重复问题。 - bitmask
1
使用 strtol() 函数,基数为 16,如何使用的其余部分在此处:https://dev59.com/TWYq5IYBdhLWcg3wzDzk - ivan.ukr
16个回答

0
我制作了一个库,可以在不使用stdio.h的情况下进行十六进制/十进制转换。非常简单易用:
unsigned hexdec (const char *hex, const int s_hex);

在进行第一次转换之前,使用以下内容初始化用于转换的数组:

void init_hexdec ();

这里是Github链接: https://github.com/kevmuret/libhex/


0

我喜欢@radhoo的解决方案,对于小系统非常高效。可以修改该解决方案以将十六进制转换为int32_t(因此是有符号值)。

/**
* hex2int
* take a hex string and convert it to a 32bit number (max 8 hex digits)
*/
int32_t hex2int(char *hex) {
    uint32_t val = *hex > 56 ? 0xFFFFFFFF : 0;
    while (*hex) {
        // get current character then increment
        uint8_t byte = *hex++; 
        // transform hex character to the 4bit equivalent number, using the ascii table indexes
        if (byte >= '0' && byte <= '9') byte = byte - '0';
        else if (byte >= 'a' && byte <='f') byte = byte - 'a' + 10;
        else if (byte >= 'A' && byte <='F') byte = byte - 'A' + 10;    
        // shift 4 to make space for new digit, and add the 4 bits of the new digit 
        val = (val << 4) | (byte & 0xF);
        }
        return val;
    }

请注意返回值为int32_t,而val仍为uint32_t以避免溢出。

 uint32_t val = *hex > 56 ? 0xFFFFFFFF : 0;

未受到对于畸形字符串的保护。


0

这里是基于"sairam singh"的解决方案的一个解决方案。那个答案是一对一的解决方案,而这个解决方案将两个ASCII半字节合并为一个字节。

// Assumes input is null terminated string.
//
//    IN                       OUT
// --------------------   --------------------
//  Offset  Hex  ASCII         Offset Hex
//  0       0x31 1             0      0x13
//  1       0x33 3                    
//  2       0x61 A             1      0xA0
//  3       0x30 0                    
//  4       0x00 NULL          2      NULL

int convert_ascii_hex_to_hex2(char *szBufOut, char *szBufIn) {

    int i = 0;  // input buffer index
    int j = 0;  // output buffer index
    char a_byte;

    // Two hex digits are combined into one byte
    while (0 != szBufIn[i]) {

        // zero result
        szBufOut[j] = 0;

        // First hex digit
        if ((szBufIn[i]>='A') && (szBufIn[i]<='F')) {
            a_byte = (unsigned int) szBufIn[i]-'A'+10;
        } else if ((szBufIn[i]>='a') && (szBufIn[i]<='f')) {
            a_byte  = (unsigned int) szBufIn[i]-'a'+10;
        } else if ((szBufIn[i]>='0') && (szBufIn[i]<='9')) {
            a_byte  = (unsigned int) szBufIn[i]-'0';
        } else {
            return -1;  // error with first digit
        }
        szBufOut[j] = a_byte << 4;

        // second hex digit
        i++;
        if ((szBufIn[i]>='A') && (szBufIn[i]<='F')) {
            a_byte = (unsigned int) szBufIn[i]-'A'+10;
        } else if ((szBufIn[i]>='a') && (szBufIn[i]<='f')) {
            a_byte  = (unsigned int) szBufIn[i]-'a'+10;
        } else if ((szBufIn[i]>='0') && (szBufIn[i]<='9')) {
            a_byte  = (unsigned int) szBufIn[i]-'0';
        } else {
            return -2;  // error with second digit
        }
        szBufOut[j] |= a_byte;

        i++;
        j++;
    }
    szBufOut[j] = 0;
    return 0;  // normal exit
}


这是我将十六进制转换为整数的解决方案。非常简单!要处理十六进制数组,可以使用“for循环”。 #include <stdio.h> int main(void) { unsigned char t = 0x8A; int c = (t >> 4) * 16 + (t & 0x0F); printf("0X8A的整数值为: %d\n", c); } - Mitchell

-1

这里是C语言的一个简单解决方案 #include<stdio.h> int main (void) { unsigned char t=0x8A; int c = (t>>4 )*16 + (t & 0x0F); printf("0X8A的整数值为: %d\n", c); }


-1

这是我将十六进制转换为整数的解决方案。非常简单!要处理十六进制数组,可以使用“for循环”。

#include<stdio.h>
int main (void)
{
     unsigned char t=0x8A;
     int c = (t>>4 )*16 + (t & 0x0F);
     printf("The integrate value of 0X8A is : %d\n", c);
}

该值应为138。


这不是被要求的内容。这段代码首先使用一个整数,将其右移4位,然后再将这些位重新加回去... 它什么也没做。 - undefined

-7

我知道这个问题很老了,但我认为解决方案看起来太复杂了。在VB中尝试这个:

Public Function HexToInt(sHEX as String) as long
Dim iLen as Integer
Dim i as Integer 
Dim SumValue as Long 
Dim iVal as long
Dim AscVal as long

    iLen = Len(sHEX)
    For i = 1 to Len(sHEX)
      AscVal = Asc(UCase(Mid$(sHEX, i,  1)))
      If AscVal >= 48 And AscVal  <= 57 Then
        iVal  = AscVal - 48
      ElseIf AscVal >= 65 And AscVal <= 70 Then
        iVal = AscVal - 55
      End If 
      SumValue = SumValue + iVal * 16 ^ (iLen- i)
    Next i 
    HexToInt  = SumValue
End Function 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接