Thread: reading hexadecimal variables

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    17

    Unhappy reading hexadecimal variables

    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

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    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.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    17
    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

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    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;
    }

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    10
    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;
    }
    www.goldroad.co.uk
    freeware ARM assembler
    dynarec gameboy advance emulator
    try them now!

  6. #6
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    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;
    }
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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);

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    17
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading float from file
    By arjunajay in forum C++ Programming
    Replies: 10
    Last Post: 07-30-2005, 11:01 PM
  2. Problems in reading binary file
    By serena in forum C Programming
    Replies: 3
    Last Post: 04-14-2005, 03:54 AM
  3. Replies: 6
    Last Post: 01-02-2004, 01:01 PM
  4. static variables
    By Luigi in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2003, 07:13 PM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 02:39 PM