简体   繁体   中英

Convert (manually) from uint32_t to 4 chars

I have a program with a special typedef, used in many place to assign unique code to components in the software.

typedef uint32_t FourCharCode;

At a point, I have a function that use such a code to do its job. I need to debug in this function

extern "C" ___DllExport void A_function(FourCharCode in_type)
{
    FourCharCode x = 'KjfG';
    FourCharCode y = 'tdkf';
    FourCharCode z = '5vpO';

    switch (in_type) {
        case x:
            // Do something
            break;
        case y:
            // Do something
            break;
        case z:
            // Do something
            break;
        default: break;
    }
    return XX::NoError;
}

Obviously, when I put a breakpoint to see what is the value of in_type , I obtain an unsigned int ('1918980169').

My question:

Is there a way, in MSVC 2013 debugger, to display the uint32_t value as something readable?

Or

What could I do manually (using a calculator, a python script or anything else useful) to retrieve the 4 chars hidden behind this uint (I am not against using a paper and a pen, but I don't see how I can do the computation)?

Maybe you can try to use an union. like that :

struct fourCharCode
{
    char fourCode[4];
};

union UFourCode
{
    fourCharCode fourChar;
    __int32_t myInt32;
};

And you use it like

int main() 
{

    UFourCode u;
    u.int32 = 1918980169; //From your question

    std::cout << u.fourChar.fourCode; 
}

The output is :

IPar

You can create a .natvis file, which specifies the display format for a native C/C++ type in the VS debugger (eg. watch window), if you wanted to display it as something other than with the default viewer. The natvis is placed in your %USERPROFILE%\\Documents\\Visual Studio 2013\\Visualizers directory, and will be loaded automatically on the next invocation of the debugger (no restart of VS required).

You can get elaborate with your natvis files, including turning them into a full plugin with installer (aka. VSIX ), however, just placing it in that directory manually is easiest, if you're not planning on wide distribution.

If you want to convert the values manually, displaying the value in hexadecimal is much easier than decimal. '5vpO' is 896954447 in decimal, but if you right-click in the Watch window, you can choose Hexadecimal Display and it shows up as 0x3576704f. 0x35 is '5' , 0x76 is 'v' , 0x70 is 'p' , and 0x4f is 'O' . Or to display as hexadecimal on a per-variable basis, append ,h to the variable name in your watch window, as in z,h .

Alternatively, you can cast the variable as a char* and display it as an array of length 4. If you add (char*)&z,4 to your Watch window, it displays "Opv5" . This is the reverse of the string as declared on x86 PCs, because they are little-endian, but is easy enough to read.

As MuertoExcobito already mentioned , you can Natvis to create a custom view of any type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM