如何在C中将字节数组转换为double? [英] How to convert a byte array into double in C?

查看:214
本文介绍了如何在C中将字节数组转换为double?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 8 个字节的字节数组,我想将它们转换并用作双精度二进制浮点数.

I've got a byte array containing 8 bytes and would like to convert and use them as a double precision binary floating-point number.

谁能告诉我如何转换它?

Could someone please tell me how to convert it?

推荐答案

试试这个:

double a;
memcpy(&a, ptr, sizeof(double));

其中 ptr 是指向您的字节数组的指针.如果你想避免复制使用联合,例如

where ptr is the pointer to your byte array. If you want to avoid copying use a union, e.g.

union {
  double d;
  char bytes[sizeof(double)];
} u;
// Store your data in u.bytes
// Use floating point number from u.d

这篇关于如何在C中将字节数组转换为double?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆