Discussion:
CString hex value to CString decimal value
(too old to reply)
Alex
2005-07-22 18:30:06 UTC
Permalink
Please bare with me, I am quite new to C++.

A CString contains a (large) hexadecimal string value,
this value needs to be converted to an decimal value,
and then put back as a string into an other CString.

Data example: hexadecimal: 93d2f666 = decimal 2480076390

I have googled and found a lot of solutions (hex string to int)
but none of them returns the correct decimal value, I suspect
that converting a large hex string to int does not work.

So if someone could point me to the right direction or
could suply a working sample, please do.

TIA

Alex
AliR
2005-07-22 18:51:07 UTC
Permalink
Your not looking hard enough.

int axtoi(const char *hexStg)
{
int n = 0; // position in string
int m = 0; // position in digit[] to shift
int count; // loop index
int intValue = 0; // integer value of hex string
int digit[5]; // hold values to convert
while (n < 4)
{
if (hexStg[n]=='\0')
break;
if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
digit[n] = hexStg[n] & 0x0f; //convert to int
else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
else break;
n++;
}
count = n;
m = n - 1;
n = 0;
while(n < count)
{
// digit[n] is value of hex digit at position n
// (m << 2) is the number of positions to shift
// OR the bits into return value
intValue = intValue | (digit[n] << (m << 2));
m--; // adjust the position to set
n++; // next digit to process
}
return (intValue);
}


AliR.
P.S. you might what to change the return type to a long instead of an int.
Post by Alex
Please bare with me, I am quite new to C++.
A CString contains a (large) hexadecimal string value,
this value needs to be converted to an decimal value,
and then put back as a string into an other CString.
Data example: hexadecimal: 93d2f666 = decimal 2480076390
I have googled and found a lot of solutions (hex string to int)
but none of them returns the correct decimal value, I suspect
that converting a large hex string to int does not work.
So if someone could point me to the right direction or
could suply a working sample, please do.
TIA
Alex
Heinz Ozwirk
2005-07-22 20:41:25 UTC
Permalink
Post by AliR
Your not looking hard enough.
int axtoi(const char *hexStg)
{
int n = 0; // position in string
int m = 0; // position in digit[] to shift
int count; // loop index
int intValue = 0; // integer value of hex string
int digit[5]; // hold values to convert
while (n < 4)
{
if (hexStg[n]=='\0')
break;
if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
What's that supposed to mean? If you want to test for characters '0' through
'9' you (or whoever invented that code) should write so. If your program
happens to run on a system using the ASCII character set (or an extension of
that) those values happen to be 0x30 and 0x39, and to test a value to be in
that range, you should use »x >= 0x30 && x <= 0x39« or »x > 0x2F && x <
0x3A« but »x >= '0' && x <= '9'« is always the better solution, except
for »isdigit(x)«.
Post by AliR
digit[n] = hexStg[n] & 0x0f; //convert to int
else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
else break;
n++;
}
count = n;
m = n - 1;
n = 0;
while(n < count)
{
// digit[n] is value of hex digit at position n
// (m << 2) is the number of positions to shift
// OR the bits into return value
intValue = intValue | (digit[n] << (m << 2));
m--; // adjust the position to set
n++; // next digit to process
}
return (intValue);
}
In which respect is that function better than strtol? What happens to
strings with more than 4 digits? What's the purpose of the array digit[5]?
Why not shifting and or'ing on the fly? And why does axtoi(";") return 11?

Heinz
AliR
2005-07-25 14:38:27 UTC
Permalink
I had completely forgotten about strtol :)
But I'll mention your suggestions to the author if I ever run into him/her.

AliR.
Post by Heinz Ozwirk
Post by AliR
Your not looking hard enough.
int axtoi(const char *hexStg)
{
int n = 0; // position in string
int m = 0; // position in digit[] to shift
int count; // loop index
int intValue = 0; // integer value of hex string
int digit[5]; // hold values to convert
while (n < 4)
{
if (hexStg[n]=='\0')
break;
if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
What's that supposed to mean? If you want to test for characters '0' through
'9' you (or whoever invented that code) should write so. If your program
happens to run on a system using the ASCII character set (or an extension of
that) those values happen to be 0x30 and 0x39, and to test a value to be in
that range, you should use »x >= 0x30 && x <= 0x39« or »x > 0x2F && x <
0x3A« but »x >= '0' && x <= '9'« is always the better solution, except
for »isdigit(x)«.
Post by AliR
digit[n] = hexStg[n] & 0x0f; //convert to int
else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
else break;
n++;
}
count = n;
m = n - 1;
n = 0;
while(n < count)
{
// digit[n] is value of hex digit at position n
// (m << 2) is the number of positions to shift
// OR the bits into return value
intValue = intValue | (digit[n] << (m << 2));
m--; // adjust the position to set
n++; // next digit to process
}
return (intValue);
}
In which respect is that function better than strtol? What happens to
strings with more than 4 digits? What's the purpose of the array digit[5]?
Why not shifting and or'ing on the fly? And why does axtoi(";") return 11?
Heinz
David Crow [MCSD]
2005-07-22 19:55:46 UTC
Permalink
Simply use:

char *pEnd;
unsigned long ulNumber = strtoul("93d2f666", &pEnd, 16);
CString str;
str.Format("%x", ulNumber);
Post by Alex
Please bare with me, I am quite new to C++.
A CString contains a (large) hexadecimal string value,
this value needs to be converted to an decimal value,
and then put back as a string into an other CString.
Data example: hexadecimal: 93d2f666 = decimal 2480076390
I have googled and found a lot of solutions (hex string to int)
but none of them returns the correct decimal value, I suspect
that converting a large hex string to int does not work.
So if someone could point me to the right direction or
could suply a working sample, please do.
TIA
Alex
Alex
2005-07-22 20:20:43 UTC
Permalink
AliR and David,

Thank you both for your replies!

Did not mention it had to be unicode aware,
but finally found it (with your suggestions):

CString str="93d2f666";
str=ConvertHexToDecimal(str);

AfxMessageBox(str) => '"2480076390"


CString ConvertHexToDecimal(const CString & input)
{
UINT v = _tcstoul(input, NULL, 16);
CString result;
result.Format(_T("%u"), v);
return result;
}

Regards,

Alex
Loading...