Convert hex string (char []) to int?

前端 未结 13 938
栀梦
栀梦 2020-11-22 12:12

I have a char[] that contains a value such as \"0x1800785\" but the function I want to give the value to requires an int, how can I convert this to an int? I have searched a

相关标签:
13条回答
  • 2020-11-22 12:51

    Have you tried strtol()?

    strtol - convert string to a long integer

    Example:

    const char *hexstring = "abcdef0";
    int number = (int)strtol(hexstring, NULL, 16);
    

    In case the string representation of the number begins with a 0x prefix, one must should use 0 as base:

    const char *hexstring = "0xabcdef0";
    int number = (int)strtol(hexstring, NULL, 0);
    

    (It's as well possible to specify an explicit base such as 16, but I wouldn't recommend introducing redundancy.)

    0 讨论(0)
  • 2020-11-22 12:51

    Something like this could be useful:

    char str[] = "0x1800785";
    int num;
    
    sscanf(str, "%x", &num);
    printf("0x%x %i\n", num, num); 
    

    Read man sscanf

    0 讨论(0)
  • 2020-11-22 12:51

    I made a librairy to make Hexadecimal / Decimal conversion without the use of stdio.h. Very simple to use :

    unsigned hexdec (const char *hex, const int s_hex);
    

    Before the first conversion intialize the array used for conversion with :

    void init_hexdec ();
    

    Here the link on github : https://github.com/kevmuret/libhex/

    0 讨论(0)
  • 2020-11-22 12:55

    I know this is really old but I think the solutions looked too complicated. Try this in 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 
    
    0 讨论(0)
  • 2020-11-22 12:56

    Or if you want to have your own implementation, I wrote this quick function as an example:

    /**
     * hex2int
     * take a hex string and convert it to a 32bit number (max 8 hex digits)
     */
    uint32_t hex2int(char *hex) {
        uint32_t val = 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;
    }
    
    0 讨论(0)
  • 2020-11-22 12:56

    Try below block of code, its working for me.

    char *p = "0x820";
    uint16_t intVal;
    sscanf(p, "%x", &intVal);
    
    printf("value x: %x - %d", intVal, intVal);
    

    Output is:

    value x: 820 - 2080
    
    0 讨论(0)
提交回复
热议问题