简体   繁体   English

如何读取使用 memcpy 的数据包

[英]How to read a packet that used memcpy

    uint8_t sendPacket[200];
    
    float a = 22.0f;  

char buffer[4] ={0x01,0x02,0x03,0x04};


    memcpy(&buffer, &a, sizeof(a));
    memcpy(sendPacket+92,buffer,sizeof(buffer));
            
    HAL_UART_Transmit(&huart7, (uint8_t*)sendPacket, length+5, 0xFFFF);

I use the code above, but I know memcpy copies the memory, but when I looked at the value in Packet, the value came out so weird我使用上面的代码,但我知道 memcpy 复制了 memory,但是当我查看 Packet 中的值时,值出来的很奇怪

There are other chords on top, but it's almost repeated, so I took it out上面还有其他的和弦,但是几乎是重复的,所以我把它拿出来了

It's supposed to be transmitted through Bluetooth, but it comes out properly in the place where it's transmitted through Bluetooth, but it comes out weird应该是通过蓝牙传输的,但是在通过蓝牙传输的地方正常出来了,但是出来的时候很奇怪

First of all, I want to send the packet in stm32 to another place to get the value, The result of the live watch is the same as in the picture首先想把stm32中的数据包发到别的地方取值,live watch的结果和图片一样

a contains float value a 包含浮点值

在此处输入图像描述

To read the float again you could for instance memcpy into a float:要再次读取浮点数,您可以例如将 memcpy 转换为浮点数:

float a_received = 0.0f;
memcpy(&a_received, &sendPacket[92], sizeof(a_received));

As user694733 pointed out, using memcpy.正如 user694733 指出的那样,使用 memcpy。

" when I looked at the value in Packet, the value came out so weird" “当我查看 Packet 中的值时,值出来的很奇怪”

When you look in sendPacket what you see is raw bytes.当您查看sendPacket时,您看到的是原始字节。 To check whether you have copied the right float at the right place you need to interpret the 4 bytes from your array to a float value.要检查您是否在正确的位置复制了正确的浮点数,您需要将数组中的 4 个字节解释为浮点值。 It is basically what a debugger or a printf would do.这基本上是调试器或printf会做的事情。

For exemple the number 22.0f is represented in IEE754 floating number representation by 0x41b00000 so you should see in your array 00 00 b0 41 for example on a little endian system.例如,数字22.0f在 IEE754 浮点数表示中由0x41b00000表示,因此您应该在数组00 00 b0 41中看到,例如在小端系统上。

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

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