Link to home
Start Free TrialLog in
Avatar of Decimal
Decimal

asked on

Equivalent to Hex()

I'm looking for a C/C++ function to convert a number to a hex string. Much like VB's Hex() command. Does C++ have a built in function for this, or do I have to create my own?
Avatar of thienpnguyen
thienpnguyen

#include <iostream>
#include <sstream>
#include <string>
using namespace std;


string toHex(long value)
{
   ostringstream os;
   os << hex << value;
   return os.str();
}
If you want to use C function, you can use itoa or _itoa (depending your compiler)

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt__itoa.2c_._i64toa.2c_._ui64toa.2c_._itow.2c_._i64tow.2c_._ui64tow.asp

    char buffer[200];

    itoa(  Number, buffer, 16 );
ASKER CERTIFIED SOLUTION
Avatar of enachemc
enachemc
Flag of Afghanistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
char buffer[20];
int k = 20;

sprintf( buffer, "%08x", k);

buffer now hold the string "00000014" which is 20 in hex. The 0 in front means "use leading zeroes" and 8 means "minimum
8 characters wide".

Similar effect can be achieved with boost's formatter library which goes well together with ostream (ostringstream, cout,
ofstream etc).

Hope this is of help.

Alf
some routines for c++ builder. Should help you if you decide to write your own

//---------------------------------------------------------------------------
String __fastcall TForm1::StrToBase16(char * Value, int Len)
{
    char H[16] = {"0123456789ABCDEF"};
    byte * pS;
    char * pD;

    String Result = "";
    if( Value == NULL)
        return Result;
    if(Len < 0)
        Len = strlen(Value);
    Result.SetLength( Len * 2);
    if( Len == 0)
        return Result;
    pD = (char*)Result.c_str();
    pS = (char*)Value;
    while( Len > 0)
    {
        *pD = H[ *pS >> 4];
        pD++;
        *pD = H[ *pS & 0X0F];
        pD++;
        pS++;
        Len--;
    }
    return Result;
}
//---------------------------------------------------------------------------
String __fastcall TForm1::Base16ToStr(char * Value, int Len)
{
    byte * pD;
    byte V;
    char * pS;
    String Result = "";
    if( Value == NULL)
        return Result;
    if(Len < 0)
        Len = strlen(Value);
    Result.SetLength((Len / 2) + 1);
    pD = (char*)Result.c_str();
    pS = (char*)Value;
    while( Len > 0)
    {
        V = (Byte)UpCase(*pS);
        pS++;
        if( V > (Byte)'9')
            *pD = V - (Byte)'A' + 10;
        else
            *pD = V - (Byte)'0';
        V = (Byte)UpCase(*pS);
        pS++;
        *pD = *pD << 4;
        if (V > (Byte)'9')
            *pD = *pD | (V - (Byte)'A' + 10);
        else
            *pD = *pD | (V - (Byte)'0');
        Len -= 2;
        pD++;
    }
    Result.SetLength( (char*)pD - (char*)Result.c_str());
    return Result;
}
Avatar of Decimal

ASKER

I forgot to mention it needs to go into a CString, otherwise thienpnguyen's answers would also have worked equally well. Thanks people.

Much simpler than what I had come up with on my own (below) :)

/////////////////////////////////////

CString intToHex(int wInt) {

CString toRetString = "";

// divide into left and right halves, solve

int left=0;
int right=0;

if ((wInt & 1) == 1) right = right + 1;
if ((wInt & 2) == 2) right = right + 2;
if ((wInt & 4) == 4) right = right + 4;
if ((wInt & 8) == 8) right = right + 8;
if ((wInt & 16) == 16) left = left + 1;
if ((wInt & 32) == 32) left = left + 2;
if ((wInt & 64) == 64) left = left + 4;
if ((wInt & 128) == 128) left = left + 8;

      switch (right)
      {

      case 0: toRetString = "0"; break;
      case 1: toRetString = "1"; break;
      case 2: toRetString = "2"; break;
      case 3: toRetString = "3"; break;
      case 4: toRetString = "4"; break;
      case 5: toRetString = "5"; break;
      case 6: toRetString = "6"; break;
      case 7: toRetString = "7"; break;
      case 8: toRetString = "8"; break;
      case 9: toRetString = "9"; break;
      case 10: toRetString = "A"; break;
      case 11: toRetString = "B"; break;
      case 12: toRetString = "C"; break;
      case 13: toRetString = "D"; break;
      case 14: toRetString = "E"; break;
      case 15: toRetString = "F"; break;
      
      }

      switch (left)
      {

      case 0: toRetString = "0" + toRetString; break;
      case 1: toRetString = "1" + toRetString; break;
      case 2: toRetString = "2" + toRetString; break;
      case 3: toRetString = "3" + toRetString; break;
      case 4: toRetString = "4" + toRetString; break;
      case 5: toRetString = "5" + toRetString; break;
      case 6: toRetString = "6" + toRetString; break;
      case 7: toRetString = "7" + toRetString; break;
      case 8: toRetString = "8" + toRetString; break;
      case 9: toRetString = "9" + toRetString; break;
      case 10: toRetString = "A" + toRetString; break;
      case 11: toRetString = "B" + toRetString; break;
      case 12: toRetString = "C" + toRetString; break;
      case 13: toRetString = "D" + toRetString; break;
      case 14: toRetString = "E" + toRetString; break;
      case 15: toRetString = "F" + toRetString; break;
      
      }

      return toRetString;
}