Re: CString hex value to CString decimal value
- From: "AliR" <AliR@xxxxxxxxxxxxx>
- Date: Fri, 22 Jul 2005 13:51:07 -0500
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.
"Alex" <alex@xxxxxxxxx> wrote in message
news:42e13b25$0$751$5fc3050@xxxxxxxxxxxxxxxxxxxxxxxxxxx
> 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
>
>
>
>
>
>
>
.
- Follow-Ups:
- Re: CString hex value to CString decimal value
- From: Heinz Ozwirk
- Re: CString hex value to CString decimal value
- References:
- CString hex value to CString decimal value
- From: Alex
- CString hex value to CString decimal value
- Prev by Date: CString hex value to CString decimal value
- Next by Date: Weird error with member variables?
- Previous by thread: CString hex value to CString decimal value
- Next by thread: Re: CString hex value to CString decimal value
- Index(es):
Relevant Pages
|