CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Aug 2003
    Posts
    938

    cahr* to WORD/DWORD conversion

    Hello, how can i do a char* to DWORD/WORD conversion?
    I saw some code that used asm in it but i don;t like to use inline asm alot in c++ since i can;t read asm too well.
    Any other posibilites on conversion?
    Thx

  2. #2
    Join Date
    Jun 2003
    Location
    not-so-Great Britain
    Posts
    178

    Re: cahr* to WORD/DWORD conversion

    reinterpret_cast should do it for DWORD.

    DWORD dw = reinterpret_cast<DWORD>(my_char*);

    but the WORD conversion I dont think is possible on a 32 bit system.
    Hungarian notation is the bane of self documenting code.
    Forget all fancy tricks with operator precedence. Code should be easily readable.
    May the BOOST be with you.
    Good free E-Books thanks to Bruce Eckel.

  3. #3
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: cahr* to WORD/DWORD conversion

    What do you need to do? It sound odd since you ask about char*, not general void*. Do you want to get the address in DWORD value? If so, why? Or want you to convert string to numeric?
    "Programs must be written for people to read, and only incidentally for machines to execute."

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: cahr* to WORD/DWORD conversion

    I'm not sure what you want but this may help you:
    Code:
    void foo(char* buf)
    {
    	DWORD dw = *(DWORD*)buf;
    
    	cout << dw << endl;
    }
    
    int main()
    {
    	DWORD dw = 44;
    
    	foo((char*)&dw);
    	
    	return 0;
    }
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: char* to WORD/DWORD conversion

    not sure if he wants a binary conversion or a lexical conversion.

  6. #6
    Join Date
    May 2005
    Posts
    15

    Re: cahr* to WORD/DWORD conversion

    strtoul()

  7. #7
    Join Date
    Aug 2003
    Posts
    938

    Re: cahr* to WORD/DWORD conversion

    here is what i wanted to do but in c++ not asm:
    Code:
    DWORD StringToDword(char *Text)
    {
    	/*
    	this function will convert and return
    	an Hexadecimel String into a real
    	DWORD hex number using assembly directive.
        */
    	
        DWORD AsmDwordNum=(DWORD)Text;
        DWORD DwordNum=0;
    	
    	/*_asm{
    		PUSHAD
    			PUSHF
    			XOR ECX,ECX
    			XOR EAX,EAX
    			XOR EDI,EDI
    			MOV EDI,8H
    			MOV ESI,AsmDwordNum
    _start:
    		MOV CL,[ESI]
    			CMP CL,30H
    			JL _lower
    			CMP CL,39H
    			JG _upper
    			SUB CL,30H
    			JMP _jmp1
    _upper:
    		SUB CL,37H
    			JMP _jmp1
    _lower:
    _jmp1:
    		ADD EAX,ECX
    			CMP EDI,1
    			JZ _out
    			SHL EAX,4H
    _out:
    		INC ESI
    			DEC EDI
    			JNZ _start
    			MOV DwordNum,EAX
    			POPF
    			POPAD
    	}
    	*/
    	return DwordNum;
    }
    I am sry , i don't know asm too well to understand this code, how can i convert it into c++?

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: cahr* to WORD/DWORD conversion

    Is this what you want to do?

    String --> Hex
    ------- -----
    "ABCD" --> 0xABCD
    "12AD" --> 0x12AD

    If so, then this is all you need to explain. No need to post assembly language or mention assembly.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Aug 2003
    Posts
    938

    Re: cahr* to WORD/DWORD conversion

    yeah pretty much but i donno how to put it into DWORD

  10. #10
    Join Date
    May 2005
    Location
    United States
    Posts
    526

    Re: cahr* to WORD/DWORD conversion

    I believe the function strtol() will do what you want. (That link will show you MSDN's page on the function.) If you're interested in the code that would accomplish such a thing manually, here's a simple example:
    Code:
    DWORD CharToDword(char *szNum)
    {
        unsigned int n;
        DWORD dwResult = 0;
    
        // process at most eight characters, so there's no overflow
        for (n = 0; n < min(8, strlen(szNum)); n++)
        {
            // shift the result up four bits (equivalent to one hex digit)
            dwResult <<= 4;
    
            // add the value of the next digit to the total
            if (szNum[n] >= '0' && szNum[n] <= '9')
                dwResult += (szNum[n] - '0');
            else if (szNum[n] >= 'A' && szNum[n] <= 'F')
                dwResult += (szNum[n] - 'A' + 10);
            else if (szNum[n] >= 'a' && szNum[n] <= 'f')
                dwResult += (szNum[n] - 'a' + 10);
            else
            {
                // invalid digit - return an error code
                // or throw an exception here
            }
        }
    
        return dwResult;
    }
    For more general code that pertains to similar problems, and/or a discussion of converting numbers from one form to another, you may want to take a look at this thread.
    Last edited by Smasher/Devourer; May 29th, 2005 at 05:14 PM.

  11. #11
    Join Date
    Aug 2003
    Posts
    938

    Re: cahr* to WORD/DWORD conversion

    what woudl eb the function for WORD then?
    if this is for DWORD, woudl tehre be any diffrence?

  12. #12
    Join Date
    May 2005
    Location
    United States
    Posts
    526

    Re: cahr* to WORD/DWORD conversion

    Quote Originally Posted by Quell
    what woudl eb the function for WORD then?
    if this is for DWORD, woudl tehre be any diffrence?
    The only difference is in the number of digits it can hold. DWORD is a 32-bit type while a WORD is only 16-bit, so if you're converting a string into a WORD, you can convert at most four hex digits.

  13. #13
    Join Date
    May 2005
    Posts
    15

    Re: cahr* to WORD/DWORD conversion

    As I said, for DWORD.. strtoul() :P

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured