Search code examples
windowshexdword

How to convert DWORD to Hex


I've searched a lot and found nothing useful regarding this thing. If anyone knows please share the knowledge.

Problem: I have a DWORD "-476053499" and I want to convert it to hex (e.g. XX XX XX XX).


Solution

  • DWORD is just a typedef for a a 32-bit unsigned integer (e.g. unsigned int or uint32_t). Notice that it's "unsigned", so I'm not sure how that plays into your value of -476053499

    You didn't specify which language, but to convert a DWORD to a hexadecimal string is easily accomplished like this in C:

    DWORD dwValue = (DWORD)(-476053499);
    char szHexString[20];
    sprintf(szHexString, "%X");  // use %x if you want lowercase hex
    

    Equivalent in C++:

    DWORD dwValue = (DWORD)(-476053499);
    std::ostringstream ss;
    ss << std::hex << dwValue;
    std::string s = ss.str();
    

    If your original value is actually a string, and not a DWORD, then to convert it to a number is simply:

    const char* s = "-476053499";
    DWORD value = (DWORD)atoi(s);