【发布时间】:2018-12-30 13:06:18
【问题描述】:
我想以双精度形式获取数据,因此之后我将数据作为 uint8_t 数组发送。于是我确定了2个steps.Steps;
1-第一步:加倍到 uint8_t
#include <stdint.h>
#include <stdio.h>
#include <string.h>
void float2Bytes(double val,uint8_t* bytes_array);
int main(void) {
double b=1690.000000;
uint8_t message[1024];
float2Bytes(b,&message[0]);
int ii;
for (ii=0; ii<8; ii++)
printf ("byteS %d is %02x\n", ii, message[ii]);
return 0;
}
void float2Bytes(double val,uint8_t* bytes_array){
// Create union of shared memory space
union {
double double_variable;
uint8_t temp_array[8];
} u;
// Overite bytes of union with float variable
u.double_variable = val;
// Assign bytes to input array
memcpy(bytes_array, u.temp_array, 8);
}
2-Second Step : uint8_t array to Double
你能在这个阶段提供建议吗?我能怎么做 ? 你能在第一阶段检查是否有错误。 ?
谢谢。
【问题讨论】:
-
你的代码在语法上看起来是正确的(虽然不能检查 rn),如果你想要评论你应该去Code Review SE :)
-
无论如何你为什么不能做相反的过程?复制联合中的字节并返回其
double成员 -
您的代码是否按预期工作?
-
附带说明:您应该将函数命名为
double2Bytes而不是float2Bytes -
第一步正在工作,但我不知道工作是否正确。我必须编写代码的第二部分以确保第一步正确与否。
标签: c casting double uint8array