 |
|
 |

The whole concept of code reuse has been trampled upon. And for what??? What advantages does it bring? Here is one line solution:
strtol({HEX_NUMBER_STRING}, NULL, 16)
|
| Sign In·View Thread·PermaLink | 3.00/5 |
|
|
|
 |
|
 |
unsigned int HexStringToUInt(char* s) { unsigned int v = 0; while (char c = *s++) { if (c < '0') return 0; //invalid character if (c > '9') //shift alphabetic characters down { if (c >= 'a') c -= 'a' - 'A'; //upper-case 'a' or higher if (c > 'Z') return 0; //invalid character if (c > '9') c -= 'A'-1-'9'; //make consecutive with digits if (c < '9' + 1) return 0; //invalid character } c -= '0'; //convert char to hex digit value v = v << 4; //shift left by a hex digit v += c; //add the current digit }
return v; }
Most of the branches can be eliminated (and thus any cost of their potentially failed prediction) with 'select' operations, depending on your processor.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
not to beat a dead horse, but if you want to use this rather than some of the slower methods suggested by others, you can eliminate the use of the variable 'firsttime', since it's only used to eliminate shifting (<<4) on the first hex digit. That one shift is probably faster than the test for firsttime (occurs for every digit), and it's OK to shift unconditioinally since shifting those zeros has no affect on the results.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
How beautyful is the Standard Library!!!!! Thank you, you save me a lot of time (and lines of code... )
|
| Sign In·View Thread·PermaLink | 4.00/5 |
|
|
|
 |
|
 |
It can also be done like this:
UINT HexToDec(const TCHAR *szValue) { UINT iValue; _stscanf(szValue, _TEXT("%x"), &iValue); return iValue; }
I think it's much cleaner and simpler
|
| Sign In·View Thread·PermaLink | 5.00/5 |
|
|
|
 |
|
 |
'swscanf' was declared deprecated
you can use also
CString s = _T("1A"); long k = wcstol( _T("0x") + s , 0, 0 );

|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
 | great!!  KIM HyunJoong | 20:05 3 Jul '06 |
|
|
 |
|
|
 |
|
 |
this will also do but it is to slow..
bool CCoder::HexEncode(BYTE*pData,TCHAR*pCode) { TCHAR buffer[3]; BYTE b=*pData;
if (b<16) { buffer[0]='0'; _itoa(b,buffer+1,16); }else _itoa(b,buffer,16);
memcpy(pCode,buffer,2); return true; }
bool CCoder::HexDecode(TCHAR*pCode,BYTE*pData) { TCHAR nr[3]; BYTE i; nr[2]='\0'; memcpy(nr,pCode,2);
sscanf(nr,"%x",&i); *pData=i;
return true; }
//Spinoza
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
 | Thanks  Codin' Carlos | 20:58 16 Oct '02 |
|
 |
It's 2am here. This converter did the trick. I didn't think about strtol either, but i can tell you i chaff at doing cstring.GetBuffer's etc, esp in unicode CE. Thanks for saving me a headache!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Yep good job, remember that strtol only returns a long so easy to overflow! Change your _httoi to return dword/unsigned long, cheers!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
I look on the net by 2 hours ago for thsi but I didn't found anything. Then I tried this: -char Str[]={"0x00f"}; -int n; -sscanf(Str,"%x",&n); now n is 15 And work. So simple.  for converting a string to int then use atoi forint or atof for float
SilverStar
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
|
 |
|
 |
Nice peace of work. I found it very helpfull. Now can you tell me how can I can I convert a normal string to an integer. I have tried StrToInt but I cant get it to work. I don't know maybe I'm stupid but it says StrToInt Undeclared identifier. Can you help.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
long BinfromHex(CString sz) { // This code is used to get the decimal value from given hexadecimal string
long m_nbinVal=0; // stores the decimal value int cnt=0; for (int i = sz.GetLength()-1; i>=0; i--) { TCHAR psz; int val; lstrcpy(&psz,(LPCTSTR)sz.Mid(i,1)); // gets the one by one each character or numeric value and assign the according no switch (psz) { case 'A' : case 'a' : val = 10; break; case 'B' : case 'b' : val = 11; break; case 'C' : case 'c' : val = 12; break; case 'D' : case 'd' : val = 13; break; case 'E' : case 'e' : val = 14; break; case 'F' : case 'f' : val = 15; break; default : val = atoi(&psz); } m_nbinVal = m_nbinVal + long(pow(16,cnt) * val); // gets the according decimal value of hexadecimal value cnt++; } return m_nbinVal; // return s the converted value }

|
| Sign In·View Thread·PermaLink | 5.00/5 |
|
|
|
 |
|
|
 |
|
 |
You're using a lookup structure, but with a for() loop to scan through it! This is very inefficient.
If you are already using a lookup, why not go all the way? Create a 256 entry lookup (actually less - you need only to go up to "f"), so, given a character you can do
value = lookup[ch];
Another, more efficient way than you present is:
if (isdigit(ch)) { value = ch - '0'; } else { value = (ch | 0x20) - 'a' + 10; }
This is assuming valid input (i.e. no "g"'s in the input, etc.)
-Oz
--- Grab WndTabs from http://www.wndtabs.com to make your VC++ experience that much more comfortable...
|
| Sign In·View Thread·PermaLink | 5.00/5 |
|
|
|
 |
|
 |
// Convert a hexencode string to a "long" value // Only pass the hex "digits"...no symbols like "0x..." static long HexToLong(CString& szHexValue) { // Make the string a set of upper case, spaceless values szHexValue.MakeUpper(); szHexValue.TrimLeft(); szHexValue.TrimRight();
// Convert the digits number to values long lDigitCount = szHexValue.GetLength(); for (long lDigit=0, lValue=0; lDigit < lDigitCount; ++lDigit) { // Make a "space" for the next hex value lValue *= 16;
// Now convert a single digit to a number TCHAR cDigit = szHexValue.GetAt(lDigit); if (cDigit >= '0' && cDigit <= '9') lValue += cDigit - '0'; else if (cDigit >= 'A' && cDigit <= 'F') lValue += 10 + cDigit - 'A'; else throw "Bad digit in Hex value string\nCStringConvert::HexToLong()"; } return lValue; }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
You could use std::stringstream, e.g.
const char* str = "0xFF";
std::stringstream stream;
stream << str;
int n;
stream >> std::hex >> n;
assert(n == 255);
|
| Sign In·View Thread·PermaLink | 3.50/5 |
|
|
|
 |
|
 |
There is a function in c to do that:
#include long strtol(const char *nptr, char **endptr, int base);
atoi(nptr) does the same as (int) strtol(nptr,(char **)NULL, 10). so just change the base (10 by 16)
see the man page at: http://www.datafocus.com/docs/man3/strtol.3.asp
|
| Sign In·View Thread·PermaLink | 5.00/5 |
|
|
|
 |
|
|
 |