将int复制到byte []的最简单方法 [英] Simplest way to copy int to byte[]
本文介绍了将int复制到byte []的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个byte [],并且正在遍历int(和其他数据)列表,我想将int复制到我的byteArray [index * 4],我该怎么做?
I have a byte[] and i am iterating through a list of ints(and other data) and i want to copy the int to my byteArray[index*4] How do i do this?
推荐答案
BitConverter
很可能是您的朋友.
BitConverter
is quite possibly your friend.
但是,通常返回一个 new 字节数组.它还不允许您指定字节序.我在 MiscUtil 中有EndianBitConverter
类,该类具有通过复制数据来转换原始类型的方法直接放入现有的字节数组中.
However, that generally returns you a new byte array. It also doesn't let you specify endianness. I have the EndianBitConverter
class in MiscUtil which has methods to convert primitive types by copying the data directly into an existing byte array.
例如:
// Copy the bytes from the integer "value" into a byte array
// (buffer) starting at index 5
EndianBitConverter.Little.CopyBytes(value, buffer, 5);
这篇关于将int复制到byte []的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文