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

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

问题描述

我有包含8个字节的字节数组,想转换,并利用它们作为双precision二进制浮点数字。

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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