Re: CString hex value to CString decimal value



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
>
>
>
>
>
>
>


.



Relevant Pages

  • Re: Is there a simpler way?
    ... Binary value or binary string? ... If you need to convert a hex string to a binary strings, ... static int setdigit; ... int SevStrHexToBin(const char *hexval, int groupchar, ...
    (microsoft.public.dotnet.languages.vc)
  • Re: 0x00FF Arrg...
    ... I think you want a hex string. ... the & automatically promotes to int so does nothing. ... http://mindprod.com Java custom programming, consulting and coaching. ...
    (comp.lang.java.programmer)
  • Re: Interesting Integer.parseInt() problem
    ... >>From some experimenting it appears parseIntis not an inverse of ... In other words the hex string to parseInt ... int back = Integer.parseInt; ...
    (comp.lang.java.programmer)
  • Re: ? Identifying Current Language Code
    ... Here is a link about convern hex string into int, ... Convert Hex String to an Integer Value. ... - by Borland Developer Support Staff ...
    (microsoft.public.vc.mfc)
  • Re: convert hex string to hex
    ... Do you mean binary type? ... Convert hex string to int: ...
    (microsoft.public.sqlserver.programming)