1

I need to convert from an unsigned long (hex) to a DWORD.

I normally would have

DWORD MyHex = 0x3F0000;

but instead I'm given

unsigned long MyLong = 3f0000;

How can I convert MyLong into MyHex?

It was fine when I was just visually printing it to a file and doing.

fprintf(pFile, "0x%X\n", MyLong);

But now I need to actually use that string as DWORD.

Biffen
  • 6,249
  • 6
  • 28
  • 36
Casper7526
  • 341
  • 3
  • 13
  • 3
    So the question is how to convert a string representation of a hex number to a `DWORD`? `unsigned long MyLong = 3f0000` won't compile so it's not at all clear. – trojanfoe Aug 28 '14 at 13:04
  • I guess we could go with converting a string rep to a dword cause I could use sprintf to convert to char[256] and have it that way I guess... – Casper7526 Aug 28 '14 at 13:08
  • 1
    Well make your mind up what it is you want. – trojanfoe Aug 28 '14 at 13:09
  • 1
    You're `unsigned long MyLong = 3f0000;` should have `0x3f0000` I would assume (since `3f0000` isn't a valid numeric constant in C)? In that case, `MyHex` and `MyLong` are already identical values. It's notquite clear what you mean when you say "convert". – lurker Aug 28 '14 at 13:10
  • Whichever way you could make it work lol. Currently the program asks for me to manually type the address everytime I compile DWORD MyOffset = 0x81231972 (I have to type this in everytime it changes) I automatically get the new offset using another program and I print it out to a file using fprintf(pFile, "0x%X\n", MyLong); So in the file it's like this 0x2B71CC4 Then I take that and manually type it inthe other program, thats why I'm trying to avoid doing, the manual part of it. – Casper7526 Aug 28 '14 at 13:12
  • So you want to know how to read the string "3f0000" from a file and calculate the DWORD value from that? – Justin Aug 28 '14 at 13:13
  • Yes Justin would certain be fine doing it that way. – Casper7526 Aug 28 '14 at 13:15

1 Answers1

0

According to the documentation, DWORD is actually just a typedef of unsigned long. There is no conversion necessary. You can use MyLong as if it were a DWORD.

eerorika
  • 232,697
  • 12
  • 197
  • 326