C#中使用指针转换数据类型[C#/unsafe]

简介:

今日因为一个同事说起,在原来的旧系统中使用指针做数据转换很方便,比如要把浮点数转化为数组,也或者是字符串的相互转换;当然,大家都知道c#中实现指针只需要写入unsafe,编译选项把“允许不安全代码”开启即可;他提出这种需求也不无道理,因为要和工控的下位机通讯,自行转换还是比较麻烦,使用指针的话将变的容易许多;具体的实现我编写了一个类,详细的作法见代码;实现了int/float/double转byte[]三种数据类型的互换,其实说白了就是C的翻版,呵呵;

可以参见MS对不安全代码的描述内容如下:
None.gif尽管实际上对 C 或 C++ 中的每种指针类型构造,C# 都设置了与之对应的引用类型,但仍然会有一些场合需要访问指针类型。例如,当需要与基础操作系统进行交互、访问内存映射设备,或实现一些以时间为关键的算法时,若没有访问指针的手段,就不可能或者至少很难完成。为了满足这样的需求,C# 提供了编写不安全代码的能力。
None.gif
None.gif在不安全代码中,可以声明和操作指针,可以在指针和整型之间执行转换,还可以获取变量的地址,等等。在某种意义上, 编写不安全代码很像在 C# 程序中编写 C 代码
None.gif
None.gif无论从开发人员还是从用户角度来看,不安全代码事实上都是一种“安全”功能。不安全代码必须用修饰符 unsafe 明确地标记,这样开发人员就不会误用不安全功能,而执行引擎将确保不会在不受信任的环境中执行不安全代码。
None.gif

另外,估计会有人说 System.Runtime.InteropServices 的Marshal类已经实现了,MS的描述如下:

None.gifMarshal类提供了一个方法集,这些方法用于分配非托管内存、复制非托管内存块、将托管类型转换为非托管类型,此外还提供了在与非托管代码交互时使用的其他杂项方法。

目前我对它还并不十分了解,这个也不是目前重点,重点是C#原来使用指针和C一样的方便,另外,熟悉指针操作的高淫或是.net类库高手,本代码免看了;

以下是源码(文末是测试代码下载),代码在公司写好了,不过没带回家,又重写了个:
None.gif using System;
None.gif using System.Runtime.InteropServices;
None.gif
None.gif namespace CSPointer
ExpandedBlockStart.gif ContractedBlock.gif dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// PointerConvert 的摘要说明。
InBlock.gif
/// 指针转换类
InBlock.gif
/// 通过指针的方式更改数据类型
InBlock.gif
/// 支持:byte <-> int/float/double
InBlock.gif
/// string 类型可以通过
InBlock.gif
/// System.Text.Encoding进行编码
InBlock.gif
/// 用途:数据传输
InBlock.gif
///
InBlock.gif
/// 作者:萧寒
InBlock.gif
/// http://www.cnblogs.com/chinasf
InBlock.gif
/// mailluck@Gmail.com
InBlock.gif
/// 最后更新日期:2005.5.27
ExpandedSubBlockEnd.gif
/// </summary>

InBlock.gifpublic unsafe class PointerConvert
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic PointerConvert()dot.gif{;}
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 转换Int数据到数组
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="data"></param>
ExpandedSubBlockEnd.gif
/// <returns></returns>

InBlock.gifpublic static byte[] ToByte(int data)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifunsafe
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifbyte* pdata = (byte*)&data;
InBlock.gifbyte[] byteArray = new byte[sizeof(int)];
InBlock.giffor (int i = 0; i < sizeof(int); ++i)
InBlock.gif byteArray[i] = *pdata++;
InBlock.gifreturn byteArray;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 转换float数据到数组
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="data"></param>
ExpandedSubBlockEnd.gif
/// <returns></returns>

InBlock.gifpublic static byte[] ToByte(float data)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifunsafe
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifbyte* pdata = (byte*)&data;
InBlock.gifbyte[] byteArray = new byte[sizeof(float)];
InBlock.giffor (int i = 0; i < sizeof(float); ++i)
InBlock.gif byteArray[i] = *pdata++;
InBlock.gifreturn byteArray;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 转换double数据到数组
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="data"></param>
ExpandedSubBlockEnd.gif
/// <returns></returns>

InBlock.gifpublic static byte[] ToByte(double data)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifunsafe
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifbyte* pdata = (byte*)&data;
InBlock.gifbyte[] byteArray = new byte[sizeof(double)];
InBlock.giffor (int i = 0; i < sizeof(double); ++i)
InBlock.gif byteArray[i] = *pdata++;
InBlock.gifreturn byteArray;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 转换数组为整形
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="data"></param>
ExpandedSubBlockEnd.gif
/// <returns></returns>

InBlock.gifpublic static int ToInt(byte[] data)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifunsafe
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifint n = 0;
InBlock.giffixed(byte* p=data)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif n = Marshal.ReadInt32((IntPtr)p);
ExpandedSubBlockEnd.gif }

InBlock.gifreturn n;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 转换数组为float
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="data"></param>
ExpandedSubBlockEnd.gif
/// <returns></returns>

InBlock.gifpublic static float ToFloat(byte[] data)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.giffloat a=0;
InBlock.gifbyte i;
InBlock.gif
InBlock.gifbyte[] x = data;
InBlock.gifvoid *pf;
InBlock.giffixed(byte* px=x)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif pf =&a;
InBlock.giffor(i=0;i<data.Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif *((byte *)pf+i)=*(px+i);
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gifreturn a;
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 转换数组为Double
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="data"></param>
ExpandedSubBlockEnd.gif
/// <returns></returns>

InBlock.gifpublic static double ToDouble(byte[] data)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifdouble a=0;
InBlock.gifbyte i;
InBlock.gif
InBlock.gifbyte[] x = data;
InBlock.gifvoid *pf;
InBlock.giffixed(byte* px=x)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif pf =&a;
InBlock.giffor(i=0;i<data.Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif *((byte *)pf+i)=*(px+i);
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gifreturn a;
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif

点这儿下载完整工程源码 



本文转自suifei博客园博客,原文链接:http://www.cnblogs.com/Chinasf/archive/2005/05/28/163932.html,如需转载请自行联系原作者
相关文章
|
1月前
|
C#
C#学习相关系列之数据类型类的三大特性(二)
C#学习相关系列之数据类型类的三大特性(二)
|
1月前
|
存储 C#
C#数据类型之结构体介绍
C#数据类型之结构体介绍
|
1月前
|
C#
C#数据类型之枚举类型
C#数据类型之枚举类型
|
1月前
|
存储 编译器 数据处理
C#基础入门之数据类型
C#基础入门之数据类型
|
4月前
|
存储 C# 图形学
【Unity 3D】C#数据类型和变量、命名规范的讲解(附源码)
【Unity 3D】C#数据类型和变量、命名规范的讲解(附源码)
51 1
|
1月前
|
存储 C#
深入C#数据类型
深入C#数据类型
5 0
|
1月前
|
C#
C#学习相关系列之数据类型类----嵌套类和嵌套方法(三)
C#学习相关系列之数据类型类----嵌套类和嵌套方法(三)
|
1月前
|
存储 C# 索引
C#学习相关系列之数据类型类的定义(一)
C#学习相关系列之数据类型类的定义(一)
|
1月前
|
存储 开发框架 .NET
C#学习相关系列之数据类型---Diactionary字典的用法
C#学习相关系列之数据类型---Diactionary字典的用法
|
3月前
|
存储 C#
C# 数据类型与类型转换:包含教程与示例
使用正确的数据类型对应于相应的变量是重要的;这样可以避免错误、节省时间和内存,还会使您的代码更易于维护和阅读。最常见的数据类型有:
30 0