如何将float数组转换为Byte数组 [英] how can I convert a float array to Byte array

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

问题描述

如何将浮点数组转换为字节数组?

how can I convert a float array to Byte array?

推荐答案

这称为序列化.您显然需要将二进制序列化为用于许多目的的字节数组.您需要使用类System.BitConverter http://msdn.microsoft.com/en- us/library/system.bitconverter.aspx [ ^ ].

它允许您使用GetBytes(Single)GetBytes(Double)将单个float元素转换为字节.
要完成整个操作,您首先需要使用GetBytes(Int32)序列化float数组的长度,然后序列化循环中的所有float成员并将它们串联在一起(例如,使用Array.Copy).立即创建长度为sizeof(Int32) + ArrayLength*sizeof(Double)(或sizeof(Single))的结果数组.

您可能需要纠正匹配的反序列化方法-从字节数组到浮点数组(使用ToInt32ToDoubleToSingle).

-SA
This is called serialization. You apparently need binary serialization into array of bytes used for many purposes. You need to use the class System.BitConverter, http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx[^].

It allows you to convert to byte a single float element using GetBytes(Single) or GetBytes(Double).

To do the whole thing, you need first serialize the length of the float array using GetBytes(Int32), then serialize all float members in loop and concatenate all together (for example, use Array.Copy). Pre-create the resulting array at once with the length sizeof(Int32) + ArrayLength*sizeof(Double) (or sizeof(Single)).

You will probably need to right a matching deserialization method — from array of byte to array of float (using ToInt32, ToDouble or ToSingle).

—SA


1.您的C#/VB/其他语言是什么?
2.请提供详细情况;您想做什么?
1. What is your language C#/VB/anything else?
2. Please provide a detail scenario; what you want to do?


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

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