Click to See Complete Forum and Search --> : cahr* to WORD/DWORD conversion


Quell
May 23rd, 2005, 06:37 PM
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

Improving
May 23rd, 2005, 06:47 PM
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.

RoboTact
May 23rd, 2005, 06:58 PM
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?

cilu
May 24th, 2005, 02:20 AM
I'm not sure what you want but this may help you:

void foo(char* buf)
{
DWORD dw = *(DWORD*)buf;

cout << dw << endl;
}

int main()
{
DWORD dw = 44;

foo((char*)&dw);

return 0;
}

NMTop40
May 24th, 2005, 03:32 AM
not sure if he wants a binary conversion or a lexical conversion.

RiZaeL
May 24th, 2005, 04:12 PM
strtoul()

Quell
May 29th, 2005, 03:18 PM
here is what i wanted to do but in c++ not asm:

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

Paul McKenzie
May 29th, 2005, 03:35 PM
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

Quell
May 29th, 2005, 04:09 PM
yeah pretty much but i donno how to put it into DWORD

Smasher/Devourer
May 29th, 2005, 04:45 PM
I believe the function strtol() (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_strtol.2c_.wcstol.asp) 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:
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 (http://www.codeguru.com/forum/showthread.php?t=342883).

Quell
May 29th, 2005, 09:34 PM
what woudl eb the function for WORD then?
if this is for DWORD, woudl tehre be any diffrence?

Smasher/Devourer
May 29th, 2005, 10:06 PM
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.

RiZaeL
June 3rd, 2005, 04:12 PM
As I said, for DWORD.. strtoul() :P