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