简体   繁体   English

将C结构转换为无符号字符

[英]Convert C structure into unsigned char

I have a function UartSend() to send data to network through uart. 我有一个函数UartSend()通过uart将数据发送到网络。 it takes argument unsigned char and an integer 它需要参数unsigned char和一个整数

UartSend(unsigned char *psend_data,int length);

i want to send a structure through this function 我想通过此功能发送结构

#pragma pack(push, 1)
struct packet
{

    int a;
    char b[3];
    ...

}PacketData;

#pragma pack(pop)

How to covert this structure to unsigned char for sending this data through UartSend? 如何通过UartSend将这种结构转换为unsigned char以便发送此数据? thanks.. 谢谢..

You can simply cast to unsigned char * , provided you get your size correct: 您可以简单地将其unsigned char *unsigned char * ,只要您的大小正确即可:

PacketData pkt;
UartSend((unsigned char *)&pkt, sizeof(pkt));

unsigned char * in this context really just means byte. 在这种情况下, unsigned char *实际上仅表示字节。

Of course, at the other end of the connection, you'll need to incorporate program logic to re-cast to PacketData (or some similar struct). 当然,在连接的另一端,您需要合并程序逻辑以重新PacketDataPacketData (或某些类似的结构)。

EDIT: However, note that as @WhozCraig points out in comments, if your destination platform is not identical to your source platform, you need to consider alignment issues (or, alternatively, rely on a GNU extension like __packed__ ) 编辑:但是,请注意,正如@WhozCraig在注释中指出的那样,如果目标平台与源平台不同,则需要考虑对齐问题(或者,依赖于__packed__类的GNU扩展)

You can use a union to access your data in the form of your struct, or of an unsigned char array. 您可以使用并集以结构或无符号char数组的形式访问数据。 This way you can avoid aliasing problems that you get with a direct casting. 这样,您可以避免直接铸造时出现的混叠问题。

Structures can have extra bytes in them to make each element aligned . 结构中可以包含额外的字节,以使每个元素对齐 Those extra bytes can vary depending on the compiler/platform being use. 这些额外的字节可能会有所不同,具体取决于所使用的编译器/平台。 As a result, what you want to do may be impossible to do. 因此,您可能想做的事情可能做不到。

I would recommend that you write a function to do the jo. 我建议您编写一个函数来执行jo。 It needs to pull each element out of the structure and send it individually. 它需要将每个元素从结构中拉出并分别发送。 A second function would be needed to receive the values, and store them back into the function. 需要第二个函数来接收值,并将它们存储回该函数中。

Remember that endianism can be an issue as well, and use the network byte ordering functions to make sure that integer values are sent in the standard format. 请记住, endianism可能是一个问题为好,并使用网络字节顺序的功能 ,以确保整数值在标准格式发送。 If the systems are different types, floating point values may need some sort of extra conversion step. 如果系统类型不同,则浮点值可能需要某种额外的转换步骤。

TipS 提示

  1. byte alignment / pack one. 字节对齐/打包一个。 recommend use Natural byte alignment, asure get the best performance. 建议使用自然字节对齐,以确保获得最佳性能。 SUN and AIX are 'Natural byte alignment' sensitive. SUN和AIX“自然字节对齐”敏感。
  2. Byte sequence conversion, normally sender send out with big-endian, receiver convert BYTES to local endian. 字节序列转换,通常发送方使用big-endian发送,接收方将BYTES转换为本地endian。 little (WINDOWS,LINUX) / big (AIX,SUN,HP) 小(WINDOWS,LINUX)/大(AIX,SUN,HP)

Your data structure being aligned so that your int falls on word boundries, which for your target might be 32 or 64 bits. 您的数据结构已对齐,以便您的int落在单词边界上,对于您的目标而言,它可能是32位或64位。

You can reorganize your struct like so so that this won't happen: struct thestruct 您可以像这样重组结构,以免发生这种情况:struct thestruct

{
 int var6;
 unsigned char var1;
 unsigned char var2;
 unsigned char var3[2];
 unsigned char var4;
 unsigned char var5[8];
 unsigned char var7[4];
};

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

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