简体   繁体   English

将32位十六进制加载到char数组中

[英]Loading 32-bit hexadecimal into char array

I am trying to load 32-bit hexadecimal into char array. 我正在尝试将32位十六进制加载到char数组中。

#define NUC 0xA8051701

unsigned char debug_msg[100];
sprintf (debug_msg, "%08x", NUC);

But it is loading only "A805" that to as ASCII character instead of hexadecimal. 但是它仅将“ A805”作为ASCII字符而不是十六进制加载。 Can any one suggest what could be the problem. 谁能建议可能是问题所在。

What I'm actually looking for is: 我实际上正在寻找的是:

debug_msg[0]=0xA8
debug_msg[1]=0x05
debug_msg[2]=0x17
debug_msg[3]=0x01

Instead of the correct value, it is loading as below: 而不是正确的值,它正在加载如下:

debug_msg[0]=0x30
debug_msg[1]=0x30
debug_msg[2]=0x30
debug_msg[3]=0x30
debug_msg[4]=0x61
debug_msg[5]=0x38
debug_msg[6]=0x30
debug_msg[7]=0x35

Effectively it is loading 0x0000a805 that to in ASCII. 实际上,它正在以ASCII格式加载0x0000a805。

On the face of it, you need: 表面上,您需要:

 debug_msg[0] = (NUC >> 24) & 0xFF;
 debug_msg[1] = (NUC >> 16) & 0xFF;
 debug_msg[2] = (NUC >>  8) & 0xFF;
 debug_msg[3] = (NUC >>  0) & 0xFF;

(where the >> 0 is optional but makes it look neater — if the compiler optimizes to omit any shift for that). (其中>> 0是可选的,但使它看起来更整洁-如果编译器进行了优化以忽略该偏移)。 If you want to handle different values in place of NUC , then: 如果要代替NUC处理不同的值,则:

unsigned long value = 0xA8051701;

debug_msg[0] = (value >> 24) & 0xFF;
debug_msg[1] = (value >> 16) & 0xFF;
debug_msg[2] = (value >>  8) & 0xFF;
debug_msg[3] = (value >>  0) & 0xFF;

Or in macro form: 或采用宏形式:

#define MANGLE(value, debug_msg) \
    debug_msg[0] = (value >> 24) & 0xFF; \
    debug_msg[1] = (value >> 16) & 0xFF; \
    debug_msg[2] = (value >>  8) & 0xFF; \
    debug_msg[3] = (value >>  0) & 0xFF

used as: 用作:

MANGLE(0xA8051701, debug_msg)

or, if you want the values at arbitrary offsets in the array: 或者,如果您希望值在数组中的任意偏移处:

#define MANGLE(value, debug_msg, offset) \
    debug_msg[offset+0] = (value >> 24) & 0xFF; \
    debug_msg[offset+1] = (value >> 16) & 0xFF; \
    debug_msg[offset+2] = (value >>  8) & 0xFF; \
    debug_msg[offset+3] = (value >>  0) & 0xFF

Used as: 用作:

MANGLE(0xA8051701, debug_msg, 24);

There might be a need to wrap the body of the macro in a do { … } while (0) loop to make it work properly after an if statement, etc. 可能需要将宏的主体包装在do { … } while (0)循环中,以使其在if语句后可以正常工作,等等。

Or you could write an inline function to do the job. 或者,您可以编写一个内联函数来完成这项工作。 Or … 要么 …

You can use a Union here 您可以在这里使用联盟

#define NUMBER_OF_BYTES_FOR_UINT    4
Union Char_UInt
{
  U8 u8data[NUMBER_OF_BYTES_FOR_UINT];
  U32 u32data;
};

Now, you can assign "NUC" (or any 32-bit number) to "u32data" and then read "u8data", It will give you byte-by-byte values. 现在,您可以将“ NUC”(或任何32位数字)分配给“ u32data”,然后读取“ u8data”,这将为您提供逐字节的值。

This is because union assigns same memory to character array and u32data. 这是因为联合为字符数组和u32data分配了相同的内存。 You write and read from same memory location but through different interfaces. 您通过相同的存储器位置但通过不同的接口进行读写。

Note that, Endianness of system plays a role here. 注意,系统的字节序在这里起作用。

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

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