12,510,981 members (59,297 online)
alternative version

402.9K views
21 bookmarked
Posted

# Convert a "Hex String" to an Integer

, 15 Jan 2001
 Rate this:
Convert a string containing a hex value to an integer
• Download demo project - 8 Kb
• <!-- Add the rest of your HTML here -->

## Convert a "Hex String" to an Integer

Sometimes I have had a string containing a Hex Value like ```char s[10] = "0xFDE8";``` that I would like to convert to an integer (in this case it would of course get the value 65000). I have not been able to find any standard C or C++ functions to do that, so I decided to write my own. It's declared as `_httoi(const TCHAR *value)` and is coded using `TCHAR` so it works both with and without `UNICODE` defined

It's possible it could be done smarter and faster, but it works and if you like it it's yours to use for free.

Here is the code to a small console application that uses the function, and demostrates its use.

```#include "stdafx.h"
#include <tchar.h>
#include <malloc.h>

int _httoi(const TCHAR *value)
{
struct CHexMap
{
TCHAR chr;
int value;
};
const int HexMapL = 16;
CHexMap HexMap[HexMapL] =
{
{'0', 0}, {'1', 1},
{'2', 2}, {'3', 3},
{'4', 4}, {'5', 5},
{'6', 6}, {'7', 7},
{'8', 8}, {'9', 9},
{'A', 10}, {'B', 11},
{'C', 12}, {'D', 13},
{'E', 14}, {'F', 15}
};
TCHAR *mstr = _tcsupr(_tcsdup(value));
TCHAR *s = mstr;
int result = 0;
if (*s == '0' && *(s + 1) == 'X') s += 2;
bool firsttime = true;
while (*s != '\0')
{
bool found = false;
for (int i = 0; i < HexMapL; i++)
{
if (*s == HexMap[i].chr)
{
if (!firsttime) result <<= 4;
result |= HexMap[i].value;
found = true;
break;
}
}
if (!found) break;
s++;
firsttime = false;
}
free(mstr);
return result;
}

int main(int argc, char* argv[])
{
TCHAR *test[4] = {_T("0xFFFF"), _T("0xabcd"), _T("ffff"), _T("ABCD")};
for (int i = 0; i < 4; i++)
_tprintf(_T("Hex String: %s is int: %d\n\r"), test[i], _httoi(test[i]));
return 0;
}
```

Well, that's all there is to it. You can either copy the code from your browser, or download the project for Visual C++ 6.0.

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

## About the Author

 Software Developer (Senior) Denmark
Huh!

## You may also be interested in...

 Pro Pro

## Comments and Discussions

 First Prev Next
 License? Member 896106510-Jul-12 2:50 Member 8961065 10-Jul-12 2:50
 Integer string (64bit) to hex having only 32 bit RomanoScuri13-Sep-10 22:41 RomanoScuri 13-Sep-10 22:41
 //---------------------------------------------------------- // // Intstring2hex.c // //---------------------------------------------------------- #include #include #include #include //---------------------------------------------------------- typedef struct { unsigned lo; unsigned hi; } int64bit; //---------------------------------------------------------- static int64bit add_int64bit(int64bit op1,int64bit op2) { int64bit sum; sum.lo = op1.lo+op2.lo; sum.hi = op1.hi+op2.hi; if ((0xFFFFFFFF - op1.lo) < op2.lo) { ++sum.hi; } return(sum); } //---------------------------------------------------------- // la moltiplicazione è ottenuta come somma (n x val = val + val +val...) // dato che "fat" dovrebbe essere un intero minore o uguale a 10 static int64bit mul_int64bit(int64bit val,int fat) { int64bit res; int i; res.lo = 0; res.hi = 0; for (i = 0; ilo = 0; pint64bit->hi = 0; val.lo = 0; val.hi = 0; for (i = (len-1); i>=0; i--) { if (isdigit(s[i])) { n = s[i]-'0'; if (i==(len-1)) { inc.hi = 0; inc.lo = n; num.hi = 0; num.lo = 1; } else { num = mul_int64bit(num,10); inc = mul_int64bit(num,n); } val = add_int64bit(val,inc); } else { return(0); } } pint64bit->lo = val.lo; pint64bit->hi = val.hi; return(1); } //---------------------------------------------------------- static int intstr2hex(char *src,char *dst) { int64bit val; if (intstr2int64bit(src,&val)) { sprintf(dst,"%08X%08X",val.hi,val.lo); return(1); } return(0); } //---------------------------------------------------------- void main(void) { char s[80]; char ss[80]; printf("? "); gets(s); if (intstr2hex(s,ss)) { printf("\n[%s] =---> [%s]\n",s,ss); } } //----------------------------------------------------------
 Oh my god- so much code and for what??? Something that is already done with one line using "strtol" TheBishop7-May-09 5:39 TheBishop 7-May-09 5:39
 efficient & no library dependencies: tektor15-Dec-08 12:04 tektor 15-Dec-08 12:04
 dead horse c. leamon11-Sep-07 4:08 c. leamon 11-Sep-07 4:08
 Why re-invent the wheel ? damnedyankee8-Dec-06 1:10 damnedyankee 8-Dec-06 1:10
 Re: Why re-invent the wheel ? Francesco Pratolongo4-Apr-07 9:22 Francesco Pratolongo 4-Apr-07 9:22
 Shorter Code nfalabi29-Oct-06 19:08 nfalabi 29-Oct-06 19:08
 Re: Shorter Code as_sound_as6-May-07 6:56 as_sound_as 6-May-07 6:56
 :) nice but... nicekr14-Aug-06 4:12 nicekr 14-Aug-06 4:12
 great!! KIM HyunJoong3-Jul-06 19:05 KIM HyunJoong 3-Jul-06 19:05
 sscanf will do too ... Anonymous27-Sep-05 3:08 Anonymous 27-Sep-05 3:08
 Re: sscanf will do too ... spinoza4-Jan-06 15:53 spinoza 4-Jan-06 15:53
 Thanks Codin' Carlos16-Oct-02 19:58 Codin' Carlos 16-Oct-02 19:58
 Re: Thanks Anonymous17-Nov-04 5:00 Anonymous 17-Nov-04 5:00
 Re: Thanks c4l23-Apr-06 11:26 c4l 23-Apr-06 11:26
 ;P If you speaking about C++ then this is the solution for converting a hexadecimal string to an integer Anonymous12-Jun-02 2:19 Anonymous 12-Jun-02 2:19
 Use strtol. Anonymous25-Jul-02 11:00 Anonymous 25-Jul-02 11:00
 String to Int Komtiki21-Apr-02 22:01 Komtiki 21-Apr-02 22:01
 Re: String to Int Anders Molin8-May-02 11:33 Anders Molin 8-May-02 11:33
 Try This also NMS10-Aug-01 0:45 NMS 10-Aug-01 0:45
 Using CString again... Liqo14-Mar-01 5:03 Liqo 14-Mar-01 5:03
 Slooooooow lookup Oz Solomonovich17-Jan-01 6:33 Oz Solomonovich 17-Jan-01 6:33
 Another way -- using CString Paul Wolfensberger14-Jan-01 5:32 Paul Wolfensberger 14-Jan-01 5:32
 std::stringstream Wilka12-Jan-01 12:33 Wilka 12-Jan-01 12:33
 Why don't you use strtol()? Nicolas P12-Jan-01 11:07 Nicolas P 12-Jan-01 11:07
 Re: Why don't you use strtol()? William E. Kempf12-Jan-01 11:35 William E. Kempf 12-Jan-01 11:35
 Re: Why don't you use strtol()? Anders Molin12-Jan-01 11:50 Anders Molin 12-Jan-01 11:50
 Re: Why don't you use strtol()? Abarend10-Oct-02 1:15 Abarend 10-Oct-02 1:15
 Re: Why don't you use strtol()? Shay Harel13-Jan-05 8:32 Shay Harel 13-Jan-05 8:32
 Re: Why don't you use strtol()? Alon Ronen18-Aug-05 1:40 Alon Ronen 18-Aug-05 1:40
 Re: Why don't you use strtol()? Shay Harel18-Aug-05 6:58 Shay Harel 18-Aug-05 6:58
 Re: Why don't you use strtol()? Alon Ronen18-Aug-05 7:04 Alon Ronen 18-Aug-05 7:04
 Re: Why don't you use strtol()? lorenzoleo5-Jul-11 23:47 lorenzoleo 5-Jul-11 23:47
 Last Visit: 31-Dec-99 18:00     Last Update: 30-Sep-16 0:45 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.