• 11-05-2001
juhigarg
Sir,

I am working on VC++ 6.0. I have used vc++ to develop a software for data aquisition. For this I am using a Data Aquisition card whose hardware settings are controllable through my software. Now If I enter the port address of the hardware device in the edit box. I am using GetWindowText(Cstring Str) function to get data from the edit box and converting it to a decimal value using atoi function. My Problem is that I want to fetch the port address as hex integer. But when I use atoi it converts the given base address as decimal address.

One way that I can solve the problem is to ask the user to enter the port address in decimal system but I don't want to do so as the manual suggests the address to be in hex format. How can I acquire the port address in hex notation.

Juhi
• 11-05-2001
Dang
Once you've used atoi to place an ascii value in an integer you have (in a signed integer) a twos complement binary value. Typically we display this value as decimal but it can be displayed anyway you want, but its still stored in binary. Binary is the only true type on a computer. So I'm not sure what you need.

If you want to represent an integer value in an ascii string as a hex value you can use _itoa( int value, char * string, int radix) where radix would be 16 for a hex value.

If none of this helps please restate the questio because I don't think I'm understanding the question.
• 11-06-2001
juhigarg
Mr. Dang,

Thanks for your prompt reply. Sir, I'll make my question more clear

Lets assume I want to address to the port having port address 200(in hex).

In this case I use the edit box and enter 200(str) as a string.

Now I use atoi on str and write
int j = atoi(str);
Now if now I see j it gives me the decimal value 200 instead of Hex 200 (which will be 512 in decimal).

Now if I convert this value back to string and write
itoa(j,str2,10) I find str2 = 200

If I write itoa(j,str2,16) I find str2 = c8(whereas I want 200 in this case).

I hope I have made my point clear.

juhi
• 11-06-2001
Dang
OK I've got it now. I'm sure there's something out there but I couldn't find it so I wrote the code below. Warning I've only tested a few cases, does not work for negative.
Code:

```#include <string.h> #include <math.h> /* Compile options needed: none */ long atolh( char * szText ); int main () {                 char szValue[] = "314";         long nValue = atolh( szValue );         return 0; } long atolh( char * szText ) {         long nHex = 0;         int nLength = strlen( szText ) - 1;         double nExp = 0;         for( int i = 0; 0 <= nLength; i++, nLength-- )         {                 nExp = pow( 16, nLength );                 nHex += ( szText[ i ] - '0' ) * static_cast< long >( nExp ); //take care of truncation warning         }         return nHex; }```
• 11-06-2001
emu_rob
Does that deal with the alpha-numeric digits in hex?

in any casel, heres the code I use, hope it helps.
needs math.h, string.h

Code:

``` long HexStringToNumber(char *token) {         static unsigned char hexconv[23]=                                         {                 0,1,2,3,4,5,6,7,8,9,        // 1,2,3,4,5,6,7,8,9                 0,0,0,0,0,0,0,10,11,        // A,B                 12,13,14,15                // C,D,E,F                 };                                         int i;         long ret=0;         int length = strlen(token);         _strupr(token);         for (i=0;i<length;i++)         {                 ret+=(long)((hexconv[token[i]-48])*(pow(16,length-1-i)));         }         return ret; }```
• 11-06-2001
Just do the following:

long HexStringToNumber(char *token)
{

static unsigned char hexconv[23]=

{
0,1,2,3,4,5,6,7,8,9, // 1,2,3,4,5,6,7,8,9
0,0,0,0,0,0,0,10,11, // A,B
12,13,14,15 // C,D,E,F
};

int i;
long ret=0;
int length = strlen(token);

_strupr(token);

for (i=0;i<length;i++)
{
ret+=(long)((hexconv[token[i]-48])*(pow(16,length-1-i)));
}

return ret;
}
• 11-06-2001
swoopy
Or maybe strtol() or strtoul() in <stdlib.h>

char *str = "ffffffff";
char *end;
long j = strtoul(str,&end,16);
printf("j:%ld\n",j);
printf("j:%lx\n",j);
• 11-06-2001
juhigarg
Thank You ,

Thanks to all of you.

But the best way to convert the string to hexadecimal was the one using strtoul and strtol . This has saved me from writting that big code for conversion.

Regards to all.

Sincerely
juhi