gpt4 book ai didi

c - 如何在不在 C 中使用 union 的情况下将十六进制字节数组连接到 long int?

转载 作者:太空宇宙 更新时间:2023-11-04 04:37:46 25 4
gpt4 key购买 nike

我有一个数组。数组字节是十六进制的。现在我试图将数组索引 1 到 4 连接到 unsigned long int,将 5 到 8 连接到另一个 unsigned long int,将索引 9 和 10 连接到 unsigned int。我试过这样。但它不起作用。我不想使用 union 来连接。我在 STM32F4-Discovery 中使用此代码。

这是一个接收中断处理函数:

  void usart1_IRqHandler()
{
unsigned char received_string[20];
unsigned long int Addr1=0,Addr2=0;
unsigned int Addr=0;

/*Code to Receive Entire packet.......
.....................................
.....................................
At this point whole packet is received, now I want to send back the response to the target node using address from received data, So first step is to concatenate address bytes*/

/*From the logic analyzer I can observe the received_string bytes as shown below
received_string[1]=0x00;
received_string[2]=0x13;
received_string[3]=0xA2;
received_string[4]=0x00;

received_string[5]=0x40;
received_string[6]=0xB4;
received_string[7]=0x14;
received_string[8]=0x35;

received_string[9]=0x8E;
received_string[10]=0xC7;*/

Addr1 = (received_string[1]<<24)| (received_string[2]<<16)| (received_string[3] <<8)| received_string[4]);
Addr2 = (received_string[5]<<24)| (received_string[6]<<16)|(received_string[7] <<8)| received_string[8]);
Addr = (received_string[9]<<8)| (received_string[10]);

Send_packet(Addr1,Addr2,Addr);
}

函数发送数据包将以这些地址为目标远程节点。

     Send_packet(unsigned long int Addr1,unsigned long int Addr1,unsigned int Addr)
{
unsigned char tx_buf[20];
tx_buf[5]=(Addr1>>24) & 0xff;
tx_buf[6]=(Addr1>>16) & 0xff;
tx_buf[7]=(Addr1>>8) & 0xff;
tx_buf[8]=(Addr1>>0) & 0xff;

tx_buf[9]=(Addr2>>24) & 0xff;
tx_buf[10]=(Addr2>>16) & 0xff;
tx_buf[11]=(Addr3>>8) & 0xff;
tx_buf[12]=(Addr4>>0) & 0xff;

tx_buf[9]=(Addr2>>8) & 0xff;
tx_buf[10]=(Addr2>>0) & 0xff;

usart_send(tx_buf);

}

这是逻辑分析仪发送数据包的输出。

  Addr1=0xA2401437
Addr2=0x025500A2
Addr=0x4014

由于地址不匹配,我无法定位到远程节点。

最佳答案

问题缺少一个 minimal reproducable example并且缺乏有关如何获取信息的详细信息。但是,如何转换数字的基本思路是正确的(问题中的代码无法编译,因此无法判断 OP 实际测试了哪些代码)。

下面是一个演示其工作原理的示例:

#include <stdio.h>

int main()
{
unsigned char received_string[20];

received_string[1] = 0x00;
received_string[2] = 0x13;
received_string[3] = 0xA2;
received_string[4] = 0x00;

received_string[5] = 0x40;
received_string[6] = 0xB4;
received_string[7] = 0x14;
received_string[8] = 0x35;

received_string[9] = 0x8E;
received_string[10] = 0xC7;

unsigned long int Addr1 = (received_string[1] << 24) | (received_string[2] << 16) |
(received_string[3] << 8) | received_string[4];
unsigned long int Addr2 = (received_string[5] << 24) | (received_string[6] << 16) |
(received_string[7] << 8) | received_string[8];
unsigned int Addr = (received_string[9] << 8) | (received_string[10]);

printf("Addr1 = %08lX\n", Addr1);
printf("Addr2 = %08lX\n", Addr2);
printf("Addr = %04X\n", Addr);

return 0;
}

这将打印:

Addr1 = 0013A200
Addr2 = 40B41435
Addr = 8EC7

注意:正如用户 M.M. 所正确陈述的那样。在commentanswer of Yasir Majeed 上,掩蔽是完全没有必要的。它要么被编译器优化掉,要么插入无用的指令。

但是请注意,为了使此代码更具可移植性,应将字节转换为 unsigned long在转移之前。否则,这将在 sizeof(int) < 4 所在的平台上失败。 .

关于c - 如何在不在 C 中使用 union 的情况下将十六进制字节数组连接到 long int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29366355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com