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.gifContractedBlock.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.gif    public unsafe class PointerConvert
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public 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.gif        public static byte[] ToByte(int data)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
unsafe 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
byte* pdata = (byte*)&data;
InBlock.gif                
byte[] byteArray = new byte[sizeof(int)];
InBlock.gif                
for (int i = 0; i < sizeof(int); ++i)
InBlock.gif                    byteArray[i] 
= *pdata++;
InBlock.gif                
return 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.gif        public static byte[] ToByte(float data)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
unsafe 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
byte* pdata = (byte*)&data;
InBlock.gif                
byte[] byteArray = new byte[sizeof(float)];
InBlock.gif                
for (int i = 0; i < sizeof(float); ++i)
InBlock.gif                    byteArray[i] 
= *pdata++;
InBlock.gif                
return 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.gif        public static byte[] ToByte(double data)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
unsafe 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
byte* pdata = (byte*)&data;
InBlock.gif                
byte[] byteArray = new byte[sizeof(double)];
InBlock.gif                
for (int i = 0; i < sizeof(double); ++i)
InBlock.gif                    byteArray[i] 
= *pdata++;
InBlock.gif                
return 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.gif        public static int ToInt(byte[] data)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
unsafe
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int n = 0;
InBlock.gif                
fixed(byte* p=data)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    n 
= Marshal.ReadInt32((IntPtr)p);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return 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.gif        public static float ToFloat(byte[] data)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
float a=0;
InBlock.gif            
byte i;
InBlock.gif            
InBlock.gif            
byte[] x = data;
InBlock.gif            
void *pf;
InBlock.gif            
fixed(byte* px=x)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                pf 
=&a;
InBlock.gif                
for(i=0;i<data.Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
*((byte *)pf+i)=*(px+i);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return 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.gif        public static double ToDouble(byte[] data)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
double a=0;
InBlock.gif            
byte i;
InBlock.gif            
InBlock.gif            
byte[] x = data;
InBlock.gif            
void *pf;
InBlock.gif            
fixed(byte* px=x)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                pf 
=&a;
InBlock.gif                
for(i=0;i<data.Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
*((byte *)pf+i)=*(px+i);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return a;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

点这儿下载完整工程源码

2005.5.28
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值