简体   繁体   English

将uint32_t(手动)转换为4个字符

[英]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的程序,该程序在很多地方用于为软件中的组件分配唯一的代码。

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'). 显然,当我放置一个断点以查看in_type的值是in_type ,我获得了一个无符号的int('1918980169')。

My question: 我的问题:

Is there a way, in MSVC 2013 debugger, to display the uint32_t value as something readable? 在MSVC 2013调试器中,有没有一种方法可以将uint32_t值显示为可读的?

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)? 我可以手动执行什么操作(使用计算器,python脚本或其他有用的工具)以检索隐藏在此uint后面的4个字符(我不反对使用纸和笔,但是我不知道该怎么做)计算)?

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. 您可以创建一个.natvis文件,该文件指定VS调试器中本机C / C ++类型的显示格式(例如,监视窗口),如果您希望将其显示为默认查看器以外的其他格式。 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). natvis放置在%USERPROFILE%\\Documents\\Visual Studio 2013\\Visualizers目录中,并且在下次调用调试器时会自动加载(无需重新启动VS)。

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. 您可以详细介绍natvis文件,包括使用安装程序(也称为VSIX )将它们转换为完整的插件,但是,如果您不打算进行广泛分发,则手动将其放在该目录中是最容易的。

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. '5vpO'十进制值为896954447,但是如果在“监视”窗口中单击鼠标右键,则可以选择“十六进制显示”,并且显示为0x3576704f。 0x35 is '5' , 0x76 is 'v' , 0x70 is 'p' , and 0x4f is 'O' . 0x35为'5' ,0x76为'v' ,0x70为'p' ,0x4f为'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 . 要在每个变量的基础上显示为十六进制,请在监视窗口的变量名后附加,h ,如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" . 或者,您可以将变量转换为char*并将其显示为长度为4的数组。如果将(char*)&z,4"Opv5"窗口,它将显示"Opv5" This is the reverse of the string as declared on x86 PCs, because they are little-endian, but is easy enough to read. 这与x86 PC上声明的字符串相反,因为它们是低位优先的,但很容易阅读。

As MuertoExcobito already mentioned , you can Natvis to create a custom view of any type. 正如MuertoExcobito 所述 ,您可以通过Natvis创建任何类型的自定义视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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