一维数组和字节数组转换 首先声明下:数组元素类型都是基础数据类型,比如:int[] i=new int[64];请问:如何将i的内容放到一个byte数组中?反之如何将一个byte数组中的信息写到i中? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 int[] i = new int[64];byte[] bytes = new byte[ i.Length * 4 ];GCHandle gch = GCHandle.Alloc(i, GCHandleType.Pinned);Marshal.Copy(gch.AddrOfPinnedObject(), bytes, 0, bytes.Length); //<-- 反之亦然gch.Free(); //uint to byte[]public static byte[] WriteUint(uint nValue) { byte[] bArray = new byte[4]; bArray[3] = (byte)(nValue & 0xFF); bArray[2] = (byte)((nValue >> 8) & 0xFF); bArray[1] = (byte)((nValue >> 16) & 0xFF); bArray[0] = (byte)((nValue >> 24) & 0xFF); return bArray; }//byte[] to uintpublic uint ReadUint(byte[] a) { byte[] bArray = new byte[4]; bArray = a; uint nValue = 0; nValue ^= bArray[0]; nValue = nValue << 8; nValue ^= bArray[1]; nValue = nValue << 8; nValue ^= bArray[2]; nValue = nValue << 8; nValue ^= bArray[3]; return nValue; } 非常感谢各位新老朋友的回贴!对于元素是基础数据类型的数组来说,Buffer.BlockCopy 是最佳方法!分不多,还请各位今后继续关照 急求高手解决 CookieContainer.SetCookies 设置问题? 如何将字符串0.00转化为数字0 这个的错误提示需要安装什么版本的水晶报表? 水果落地游戏 奇怪的问题...关于DataGrid的 ???关于软件的注册?!! 请高手们帮帮我啊,用C#实现一个TCP/UDP通信调试工具? 获取论坛和网站模板设计理念 entity framework怎么查询一条新增加的数据,在未执行savechanges时? GridControl 行颜色 母窗口和子窗口的问题,高手来看 ClickOnce在哪里?以及怎么用?
int[] i = new int[64];
byte[] bytes = new byte[ i.Length * 4 ];
GCHandle gch = GCHandle.Alloc(i, GCHandleType.Pinned);
Marshal.Copy(gch.AddrOfPinnedObject(), bytes, 0, bytes.Length); //<-- 反之亦然
gch.Free();
public static byte[] WriteUint(uint nValue)
{
byte[] bArray = new byte[4];
bArray[3] = (byte)(nValue & 0xFF);
bArray[2] = (byte)((nValue >> 8) & 0xFF);
bArray[1] = (byte)((nValue >> 16) & 0xFF);
bArray[0] = (byte)((nValue >> 24) & 0xFF);
return bArray;
}
//byte[] to uint
public uint ReadUint(byte[] a)
{
byte[] bArray = new byte[4];
bArray = a;
uint nValue = 0;
nValue ^= bArray[0];
nValue = nValue << 8;
nValue ^= bArray[1];
nValue = nValue << 8;
nValue ^= bArray[2];
nValue = nValue << 8;
nValue ^= bArray[3];
return nValue;
}
分不多,还请各位今后继续关照