IEEE754浮点数指针方式转换程序 using System; using System.Runtime.InteropServices; namespace CSPointer ... { /**//**//**//// <summary> /// PointerConvert 的摘要说明。 /// 指针转换类 /// 通过指针的方式更改数据类型 /// 支持:byte <-> int/float/double /// string 类型可以通过 /// System.Text.Encoding进行编码 /// 用途:数据传输 /// /// 作者:萧寒 /// http://www.cnblogs.com/chinasf /// mailluck@Gmail.com /// 最后更新日期:2005.5.27 /// </summary> public unsafe class PointerConvert ...{ public PointerConvert()...{;} /**//**//**//// <summary> /// 转换Int数据到数组 /// </summary> /// <param name="data"></param> /// <returns></returns> public static byte[] ToByte(int data) ...{ unsafe ...{ byte* pdata = (byte*)&data; byte[] byteArray = new byte[sizeof(int)]; for (int i = 0; i < sizeof(int); ++i) byteArray[i] = *pdata++; return byteArray; } } /**//**//**//// <summary> /// 转换float数据到数组 /// </summary> /// <param name="data"></param> /// <returns></returns> public static byte[] ToByte(float data) ...{ unsafe ...{ byte* pdata = (byte*)&data; byte[] byteArray = new byte[sizeof(float)]; for (int i = 0; i < sizeof(float); ++i) byteArray[i] = *pdata++; return byteArray; } } /**//**//**//// <summary> /// 转换double数据到数组 /// </summary> /// <param name="data"></param> /// <returns></returns> public static byte[] ToByte(double data) ...{ unsafe ...{ byte* pdata = (byte*)&data; byte[] byteArray = new byte[sizeof(double)]; for (int i = 0; i < sizeof(double); ++i) byteArray[i] = *pdata++; return byteArray; } } /**//**//**//// <summary> /// 转换数组为整形 /// </summary> /// <param name="data"></param> /// <returns></returns> public static int ToInt(byte[] data) ...{ unsafe ...{ int n = 0; fixed(byte* p=data) ...{ n = Marshal.ReadInt32((IntPtr)p); } return n; } } /**//**//**//// <summary> /// 转换数组为float /// </summary> /// <param name="data"></param> /// <returns></returns> public static float ToFloat(byte[] data) ...{ float a=0; byte i; byte[] x = data; void *pf; fixed(byte* px=x) ...{ pf =&a; for(i=0;i<data.Length;i++) ...{ *((byte *)pf+i)=*(px+i); } } return a; } /**//**//**//// <summary> /// 转换数组为Double /// </summary> /// <param name="data"></param> /// <returns></returns> public static double ToDouble(byte[] data) ...{ double a=0; byte i; byte[] x = data; void *pf; fixed(byte* px=x) ...{ pf =&a; for(i=0;i<data.Length;i++) ...{ *((byte *)pf+i)=*(px+i); } } return a; } } }