Convert hexadecimal string (char []) to int?

I have a char [] that contains a value such as "0x1800785", but the function I want to provide for the value requires an int, how can I convert this to an int? I searched around but can't find an answer. Thank.

+71
c char int
Apr 14 2018-12-18T00:
source share
11 answers

Have you tried strtol() ?

strtol - convert string to long integer

Example:

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

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

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

(You can also specify an explicit base, such as 16, but I would not recommend introducing redundancy.)

+183
Apr 14 '12 at 19:01
source share

Something like this might be useful:

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

Read man sscanf

+19
Apr 14 '12 at 19:14
source share

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; } 
+14
Aug 20 '16 at 10:19
source share

Assuming you mean a string, how about strtol ?

+10
Apr 14 '12 at 19:01
source share

Try using a block of code that works for me.

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

Exit:

 value x: 820 - 2080 
+4
Sep 05 '14 at 8:41
source share

So, after some time searching and finding that strtol is pretty slow, I encoded my own function. It only works for uppercase letters, but adding lowercase letters is not a problem.

 int hexToInt(PCHAR _hex, int offset = 0, int size = 6) { int _result = 0; DWORD _resultPtr = reinterpret_cast<DWORD>(&_result); for(int i=0;i<size;i+=2) { int _multiplierFirstValue = 0, _addonSecondValue = 0; char _firstChar = _hex[offset + i]; if(_firstChar >= 0x30 && _firstChar <= 0x39) _multiplierFirstValue = _firstChar - 0x30; else if(_firstChar >= 0x41 && _firstChar <= 0x46) _multiplierFirstValue = 10 + (_firstChar - 0x41); char _secndChar = _hex[offset + i + 1]; if(_secndChar >= 0x30 && _secndChar <= 0x39) _addonSecondValue = _secndChar - 0x30; else if(_secndChar >= 0x41 && _secndChar <= 0x46) _addonSecondValue = 10 + (_secndChar - 0x41); *(BYTE *)(_resultPtr + (size / 2) - (i / 2) - 1) = (BYTE)(_multiplierFirstValue * 16 + _addonSecondValue); } return _result; } 

Using:

 char *someHex = "#CCFF00FF"; int hexDevalue = hexToInt(someHex, 1, 8); 

1, because the hex that we want to convert starts at offset 1, and 8, because this is the length of the hex.

Speedtest (1,000,000 calls):

 strtol ~ 0.4400s hexToInt ~ 0.1100s 
+3
Jan 05 '18 at 17:32
source share

I did librairy to do the hex / decimal conversion without using stdio.h . Very easy to use:

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

Before the first conversion, initialize the array used for the conversion with:

 void init_hexdec (); 

Here's a link to github: https://github.com/kevmuret/libhex/

0
May 03 '14 at 1:41
source share

I did a similar thing, think it can help it really works for me

 int main(){ int co[8],i;char ch[8];printf("please enter the string:");scanf("%s",ch);for(i=0;i<=7;i++){if((ch[i]>='A')&&(ch[i]<='F')){co[i]=(unsigned int)ch[i]-'A'+10;}else if((ch[i]>='0')&&(ch[i]<='9')){co[i]=(unsigned int)ch[i]-'0'+0;}} 

here I took only a string of 8 characters. if you want u to be able to add similar logic for 'a' to 'f' in order to give its equivalent hex values, I did not do this because I did not need it.

0
Jul 02 '16 at 5:14
source share

Use strtol if you have libc, like strtol in the top answer. However, if you like non-standard things or you use a microcontroller without libc or so, you might want a slightly optimized version without complex branching.

 #include <inttypes.h> /** * xtou64 * Take a hex string and convert it to a 64bit number (max 16 hex digits). * The string must only contain digits and valid hex characters. */ uint64_t xtou64(const char *str) { uint64_t res = 0; char c; while ((c = *str++)) { char v = (c & 0xF) + (c >> 6) | ((c >> 3) & 0x8); res = (res << 4) | (uint64_t) v; } return res; } 

The magic of shifting bits is as follows: just use the last 4 bits, but if it's not a digit, then also add 9.

0
Jul 19 '19 at 12:34
source share

Use xtoi (stdlib.h). The string has "0x" as the first two indices, so trim val [0] and val [1] is turned off by sending xtoi & val [2].

xtoi( &val[2] );

-one
Sep 08 '16 at 2:59
source share

I know this is really old, but I think the solutions looked too complicated. Try this on 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 
-2
Jun 21 '18 at 14:53
source share



All Articles