读书人

int 与 byte[] 的互相转换

发布时间: 2012-12-21 12:03:49 作者: rapoo

int 与 byte[] 的相互转换

http://www.cnblogs.com/ly4cn/archive/2005/09/08/232523.html

?

int 与 byte[] 的互相转换b?=?new?byte[]?int 与 byte[] 的互相转换{0xfe,0x5a,0x11,0xfa};
int 与 byte[] 的互相转换u?=?(uint)(b[0]?|?b[1]?<<?8?|?b[2]?<<?16?|?b[3]?<<?24); 从int 到 byte[]
int 与 byte[] 的互相转换b[0]?=?(byte)(u);
int 与 byte[] 的互相转换b[1]?=?(byte)(u?>>?8);
int 与 byte[] 的互相转换b[2]?=?(byte)(u?>>?16);
int 与 byte[] 的互相转换b[3]?=?(byte)(u?>>?24);
int 与 byte[] 的互相转换

2. 使用 BitConverter (强力推荐)

从int 到byte[]
int 与 byte[] 的互相转换byte[]?b?=?BitConverter.GetBytes(
int 与 byte[] 的互相转换???0xba5eba11?);?
int 与 byte[] 的互相转换//{0x11,0xba,0x5e,0xba} ? 从byte[]到int
int 与 byte[] 的互相转换uint?u?=?BitConverter.ToUInt32(
int 与 byte[] 的互相转换int 与 byte[] 的互相转换???new?byte[]?int 与 byte[] 的互相转换{0xfe,?0x5a,?0x11,?
int 与 byte[] 的互相转换???0xfa},0?);?//?0xfa115afe

3. Unsafe代码 (虽然简单,但需要更改编译选项)

int 与 byte[] 的互相转换unsafe?int 与 byte[] 的互相转换int 与 byte[] 的互相转换int 与 byte[] 的互相转换{int 与 byte[] 的互相转换//?从int?到byte[]int 与 byte[] 的互相转换fixed?(?byte*?pb?=?b?);int 与 byte[] 的互相转换//?从byte[]?到?intint 与 byte[] 的互相转换u?=?*((uint*)pb);int 与 byte[] 的互相转换}
4. 使用Marshal类??? 
int 与 byte[] 的互相转换IntPtr?ptr?=?Marshal.AllocHGlobal(4);?//?要分配非托管内存
int 与 byte[] 的互相转换int 与 byte[] 的互相转换????byte[]?b=?new?byte[4]int 与 byte[] 的互相转换{1,2,3,4};
int 与 byte[] 的互相转换//从byte[]?到?int
int 与 byte[] 的互相转换????Marshal.Copy(b,?0,?ptr,?4);
int 与 byte[] 的互相转换????int?u?=?Marshal.ReadInt32(ptr);?
int 与 byte[] 的互相转换//从int?到byte[]
int 与 byte[] 的互相转换????Marshal.WriteInt32(ptr,?u);
int 与 byte[] 的互相转换????Marshal.Copy(ptr,b,0,4);
int 与 byte[] 的互相转换????Marshal.FreeHGlobal(ptr);?//?最后要记得释放内存

?

??? 使用第4种看起来比较麻烦,实际上,如果想把结构(struct)类型转换成byte[],则第4种是相当方便的。例如:

int 与 byte[] 的互相转换???int?len?=?Marshal.Sizeof(typeof(MyStruct));
int 与 byte[] 的互相转换????MyStruct?o;
int 与 byte[] 的互相转换????byte[]?arr?=?new?byte[len];//{int 与 byte[] 的互相转换};
int 与 byte[] 的互相转换
int 与 byte[] 的互相转换????IntPtr?ptr?=?Marshal.AllocHGlobal(len);
int 与 byte[] 的互相转换????try
int 与 byte[] 的互相转换int 与 byte[] 的互相转换????int 与 byte[] 的互相转换{
int 与 byte[] 的互相转换//?从byte[]?到struct?MyStruct
int 与 byte[] 的互相转换?????Marshal.Copy(arr,?index,?ptr,?Math.Min(length,?arr.Length?-?index));
int 与 byte[] 的互相转换?????o?=?(MyStruct)Marshal.PtrToStructure(ptr,?typeof(MyStruct));
int 与 byte[] 的互相转换
int 与 byte[] 的互相转换
int 与 byte[] 的互相转换//?从struct?MyStruct?到?byte[]
int 与 byte[] 的互相转换?????Marshal.StructureToPtr(o,?ptr,?true);?//?使用时要注意fDeleteOld参数
int 与 byte[] 的互相转换?????Marshal.Copy(ptr,?arr,?0,?len);
int 与 byte[] 的互相转换????}
int 与 byte[] 的互相转换????finally
int 与 byte[] 的互相转换int 与 byte[] 的互相转换????int 与 byte[] 的互相转换{
int 与 byte[] 的互相转换?????Marshal.FreeHGlobal(ptr);
int 与 byte[] 的互相转换????}
int 与 byte[] 的互相转换????return?o;
int 与 byte[] 的互相转换

?

?

? public byte[] intToByte(int i) {??

02 byte[] bt = new byte[4];??

03 bt[0] = (byte) (0xff & i);??

04 bt[1] = (byte) ((0xff00 & i) >> 8);??

05 bt[2] = (byte) ((0xff0000 & i) >> 16);??

06 bt[3] = (byte) ((0xff000000 & i) >> 24);??

07 return bt;??

08 }??

09 public? static int bytesToInt(byte[] bytes) {??

10 int num = bytes[0] & 0xFF;??

11 num |= ((bytes[1] << 8) & 0xFF00);??

12 num |= ((bytes[2] << 16) & 0xFF0000);??

13 num |= ((bytes[3] << 24) & 0xFF000000);??

14 return num;??

15 }


文章出处:飞诺网(www.firnow.com):http://dev.firnow.com/course/3_program/java/javajs/20090208/154987.html

读书人网 >编程

热点推荐