int 与 byte[] 的相互转换
来源:互联网 发布:国研网统计数据库包括 编辑:程序博客网 时间:2024/05/25 08:15
int 与 byte[] 的相互转换
关于 int 与 byte[] 的相互转换,Mattias Sjogren 介绍了3种方法。请参见 《将Integer转换成Byte Array》。其实应该还有不少方法。在这里,我归纳了包括Mattias Sjogren在内的4种方法。
int 与 byte[] 的相互转换 沐枫网志
1. 最普通的方法
- 从byte[] 到 uint
b = new byte[] {0xfe,0x5a,0x11,0xfa};
u = (uint)(b[0] | b[1] << 8 | b[2] << 16 | b[3] << 24); - 从int 到 byte[]
b[0] = (byte)(u);
b[1] = (byte)(u >> 8);
b[2] = (byte)(u >> 16);
b[3] = (byte)(u >> 24);
2. 使用 BitConverter (强力推荐)
- 从int 到byte[]
byte[] b = BitConverter.GetBytes(
0xba5eba11 );
//{0x11,0xba,0x5e,0xba} - 从byte[]到int
uint u = BitConverter.ToUInt32(
new byte[] {0xfe, 0x5a, 0x11,
0xfa},0 ); // 0xfa115afe
3. Unsafe代码 (虽然简单,但需要更改编译选项)
unsafe
{
// 从int 到byte[]
fixed ( byte* pb = b );
// 从byte[] 到 int
u = *((uint*)pb);
}
4. 使用Marshal类
IntPtr ptr = Marshal.AllocHGlobal(4); // 要分配非托管内存
byte[] b= new byte[4]{1,2,3,4};
//从byte[] 到 int
Marshal.Copy(b, 0, ptr, 4);
int u = Marshal.ReadInt32(ptr);
//从int 到byte[]
Marshal.WriteInt32(ptr, u);
Marshal.Copy(ptr,b,0,4);
Marshal.FreeHGlobal(ptr); // 最后要记得释放内存
byte[] b= new byte[4]{1,2,3,4};
//从byte[] 到 int
Marshal.Copy(b, 0, ptr, 4);
int u = Marshal.ReadInt32(ptr);
//从int 到byte[]
Marshal.WriteInt32(ptr, u);
Marshal.Copy(ptr,b,0,4);
Marshal.FreeHGlobal(ptr); // 最后要记得释放内存
使用第4种看起来比较麻烦,实际上,如果想把结构(struct)类型转换成byte[],则第4种是相当方便的。例如:
int len = Marshal.Sizeof(typeof(MyStruct));
MyStruct o;
byte[] arr = new byte[len];//{};
IntPtr ptr = Marshal.AllocHGlobal(len);
try
{
// 从byte[] 到struct MyStruct
Marshal.Copy(arr, index, ptr, Math.Min(length, arr.Length - index));
o = (MyStruct)Marshal.PtrToStructure(ptr, typeof(MyStruct));
// 从struct MyStruct 到 byte[]
Marshal.StructureToPtr(o, ptr, true); // 使用时要注意fDeleteOld参数
Marshal.Copy(ptr, arr, 0, len);
}
finally
{
Marshal.FreeHGlobal(ptr);
}
return o;
MyStruct o;
byte[] arr = new byte[len];//{};
IntPtr ptr = Marshal.AllocHGlobal(len);
try
{
// 从byte[] 到struct MyStruct
Marshal.Copy(arr, index, ptr, Math.Min(length, arr.Length - index));
o = (MyStruct)Marshal.PtrToStructure(ptr, typeof(MyStruct));
// 从struct MyStruct 到 byte[]
Marshal.StructureToPtr(o, ptr, true); // 使用时要注意fDeleteOld参数
Marshal.Copy(ptr, arr, 0, len);
}
finally
{
Marshal.FreeHGlobal(ptr);
}
return o;
posted on 2005-09-08 15:33 沐枫 阅读(2944) 评论(4) 编辑 收藏 所属分类: .NET
- int 与 byte[] 的相互转换
- int 与 byte[] 的相互转换
- int 与 byte[] 的相互转换
- int 与 byte[] 的相互转换
- C++ int 与 byte[] 的相互转换
- int、char、double与byte相互转换的程序
- int、char、double与byte相互转换的程序
- int、char、double与byte相互转换的程序
- [.NET]int 与 byte[] 的相互转换【转】
- int、char、double与byte相互转换的程序
- int、char、double与byte相互转换的程序
- int、char、double与byte相互转换的程序
- int、char、double与byte相互转换的程序
- java int与byte[]数组的相互转换 补码
- 整型变量(int)与字节数组(byte[])的相互转换
- java中int与byte的相互转换
- java中int与byte的相互转换
- NSData 与NSString,Byte数组,UIImage,int的相互转换
- 什么是JAVA内容仓库(Java Content Repository)(4完)
- 嵌入式Linux操作系统学习规划
- 噩梦的解析
- Presenting Time-Based Media with JMF(翻译自JMF)
- IE中打印预览
- int 与 byte[] 的相互转换
- FCKeditor编辑器的使用
- 如何建立自信心
- 智商VS情商 IQ VS EQ
- 让你的每一天多产高效
- 像职业赌徒一样理财:从小处着手
- javascript的定时操作(转)
- 15个步骤收获一生的学习习惯
- 九步以确定你的人生目标和制定达到目标的计划
Feedback