giovedì 7 agosto 2008

Convert string to Hex

A chunk of code how to convert string (unicode or not) to long Hexadecimal (base-16)

CString sUnicode = _T("A123F");

CStringA sASCII(sUnicode);
char * pNotConverted=NULL;
long lHex = strtol (sASCII.operator LPCSTR(),&pNotConverted,16);
return false;

  • For unicode directly without conversion use wcstol the wide-character version
  • For better range (__int64) use _strtoi64 or _wcstoi64

Remember the long limits

Type Name


Range of Values

long4–2,147,483,648 to 2,147,483,647
__int648–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Nessun commento: