二月 22, 2013

C#中的几种数据类型转换

Written by

1、string to btye[]

// string to byte[]
string str = "abc";
byte[] data = System.Text.Encoding.Default.GetBytes(str);

 

2、 byte[] to short[] 和 short[] to byte[]

// byte[] to short[]
short[] shortArray = new short[compressedData.Length * Marshal.SizeOf(compressedData[0])];
IntPtr ptrByteArray = Marshal.UnsafeAddrOfPinnedArrayElement(compressedData, 0);
Marshal.Copy(ptrByteArray, shortArray, 0, shortArray.Length);

// short[] to byte[]
byte[] byteArray = new byte[u16buf.Length * Marshal.SizeOf(u16buf[0])];
IntPtr ptrShortArray = Marshal.UnsafeAddrOfPinnedArrayElement(u16buf, 0);
Marshal.Copy(ptrShortArray, byteArray, 0, byteArray.Length);

3、截取byte[]中的一部分给另一个byte[]

byte[] buff = new byte[100];
byte[] compressedData = new byte[30];
Array.Copy(buff, 4, compressedData, 0, 30);

 

Category : 其他

Tags :

Comments

2 Responses

  1. Javier说道:

    I guess finding useful, reliable inoaomrtifn on the internet isn’t hopeless after all.

  2. Adeeb说道:

    You’re on top of the game. Thanks for shgirna.

发表评论

电子邮件地址不会被公开。

Proudly powered by WordPress and Sweet Tech Theme