C#命名管道中int型数组和字符数组的转换问题
先之 2008-06-06 09:33:32 命名管道中,如何传递int型数组呢?即int型数组和字符数组转换问题。
命名管道中要把一个数据写入管道,那么需要调用下面的API函数,如下:
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool WriteFile(
IntPtr hHandle, // handle to file
byte[] lpBuffer,// data buffer
uint nNumberOfBytesToWrite, // number of bytes to write
byte[] lpNumberOfBytesWritten, // number of bytes written
uint lpOverlapped // overlapped buffer
);[/align]
所有的数据必须转换为字符数组的形式:byte[] lpBuffer,
如果是简单的基本类型,如int型,可以通过System.BitConverter.GetBytes(intdata),将int型数组转换为byte[],
读取数据时反过来通过System.BitConverter.ToInt32(bytes,0)将字符数组转换为int型。.net库函数提供了将基本类型数据
转换为字符数组的函数,但是没有提供转换复杂类型的函数。
所以,如果传递的是其他非基本类型数据,如int型数组,该如何转换呢?