Click here to Skip to main content
15,888,816 members
Articles / Programming Languages / C++
Article

Convert a "Hex String" to an Integer

Rate me:
Please Sign up or sign in to vote.
3.41/5 (15 votes)
15 Jan 2001 450K   1.8K   23   34
Convert a string containing a hex value to an integer
  • Download demo project - 8 Kb
  • 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.

    License

    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


    Written By
    Software Developer (Senior)
    Denmark Denmark
    Huh! Wink | ;-)

    Comments and Discussions

     
    QuestionWhy don't you use strtol()? Pin
    12-Jan-01 11:07
    suss12-Jan-01 11:07 
    AnswerRe: Why don't you use strtol()? Pin
    William E. Kempf12-Jan-01 11:35
    William E. Kempf12-Jan-01 11:35 
    AnswerRe: Why don't you use strtol()? Pin
    Anders Molin12-Jan-01 11:50
    professionalAnders Molin12-Jan-01 11:50 
    GeneralRe: Why don't you use strtol()? Pin
    Barend Gehrels10-Oct-02 1:15
    Barend Gehrels10-Oct-02 1:15 
    AnswerRe: Why don't you use strtol()? Pin
    Shay Harel13-Jan-05 8:32
    Shay Harel13-Jan-05 8:32 
    GeneralRe: Why don't you use strtol()? Pin
    Alon Ronen18-Aug-05 1:40
    Alon Ronen18-Aug-05 1:40 
    GeneralRe: Why don't you use strtol()? Pin
    Shay Harel18-Aug-05 6:58
    Shay Harel18-Aug-05 6:58 
    GeneralRe: Why don't you use strtol()? Pin
    Alon Ronen18-Aug-05 7:04
    Alon Ronen18-Aug-05 7:04 
    AnswerRe: Why don't you use strtol()? Pin
    lorenzoleo5-Jul-11 23:47
    lorenzoleo5-Jul-11 23:47 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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