|
-
May 23rd, 2005, 06:37 PM
#1
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
-
May 23rd, 2005, 06:47 PM
#2
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.
-
May 23rd, 2005, 06:58 PM
#3
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."
-
May 24th, 2005, 02:20 AM
#4
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;
}
-
May 24th, 2005, 03:32 AM
#5
Re: char* to WORD/DWORD conversion
not sure if he wants a binary conversion or a lexical conversion.
-
May 24th, 2005, 04:12 PM
#6
Re: cahr* to WORD/DWORD conversion
-
May 29th, 2005, 03:18 PM
#7
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++?
-
May 29th, 2005, 03:35 PM
#8
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
-
May 29th, 2005, 04:09 PM
#9
Re: cahr* to WORD/DWORD conversion
yeah pretty much but i donno how to put it into DWORD
-
May 29th, 2005, 04:45 PM
#10
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.
-
May 29th, 2005, 09:34 PM
#11
Re: cahr* to WORD/DWORD conversion
what woudl eb the function for WORD then?
if this is for DWORD, woudl tehre be any diffrence?
-
May 29th, 2005, 10:06 PM
#12
Re: cahr* to WORD/DWORD conversion
 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.
-
June 3rd, 2005, 04:12 PM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|