On Tue, 14 Sep 2004 16:57:42 -0700, Jim Duffy wrote:
> Hi All,
>
> Does anyone have some sample code that demonstrates how to convert a hex
>
> string to an int value?
>
> For example I need to convert a string like "3F" to an Int16 with a value
> of
>
> 63
>
> Can't find any API call that could help with this.
>
> Thanks in advance..
>
> Jim
This works for me. Just pass a base of 16 to it:
UInt32 strToInt(Char* strin, UInt16 base)
{
UInt16 len;
UInt16 c;
UInt32 conv_val;
UInt32 mult;
UInt16 x;
UInt16 err=1;
conv_val=0;
len = StrLen(strin);
c=0;
while(c<len-1)
{
// this calculates the multiplier for each digit position
mult=base;
x=len-c;
while(x>2)
{
mult = mult*base;
x--;
}
if(TxtCharIsDigit(strin[c])){
conv_val = conv_val+(strin[c]-48)*mult;
if((strin[c]-48)>base-1)err=0;
}
if(TxtCharIsAlpha(strin[c])){
if(TxtCharIsLower(strin[c])){
conv_val = conv_val+(strin[c]-87)*mult;
if((strin[c]-87)>base-1)err=0;
}
else {
conv_val = conv_val+(strin[c]-55)*mult;
if((strin[c]-55)>base-1)err=0;
}
}
c++;
}
if(TxtCharIsDigit(strin[len-1])){
conv_val = conv_val+strin[len-1]-48;
if((strin[len-1]-48)>base-1)err=0;
}
if(TxtCharIsAlpha(strin[len-1])){
if(TxtCharIsLower(strin[len-1])){
conv_val = conv_val+strin[len-1]-87;
if((strin[len-1]-87)>base-1)err=0;
}
else {
conv_val = conv_val+strin[len-1]-55;
if((strin[len-1]-55)>base-1)err=0;
}
}
if(err==0)conv_val=0;
return conv_val;
}
Terry
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/support/forums/