C#从字节数组读取基础数据
C#的byte[]和AS3中的ByteArray都是字节数组.但是明显的AS3的ByteArray更加好⽤⼀些.因为在ByteArray当中有⼀个position属性,可以读取相应的字节后,⾃动指向下⼀个没有读取的字节的index.这样你永远不⽤⾃⼰再建⼀个index来⼿动的处理这件事情了.当然,ByteArray还有其他的⼀些⽅法和属性,是byte[]没有的.我这⾥强调,并⾮贬低C#,只是在这⼀块,需要做⼀些多余的事情,显得相当的繁琐.为此我封装了⼀个类库,核⼼类 BytesDecode 如下:
//=====================================================================
//
//  All right reserved
//  filename : BytesDecode
//  description :
typeof array//
//  create by User at 2016/8/12 9:57:15
//=====================================================================
using System;
using System.Runtime.InteropServices;
namespace BytesLib
{
/// <summary>
/// 解析字节流
/// </summary>
internal sealed class BytesDecode
{
/// <summary>
/
// 解析基础值类型
/// </summary>
/// <typeparam name="T">值类型</typeparam>
/// <param name="bytes">字节流数组</param>
/// <param name="index">开始Index</param>
/// <returns></returns>
public T getStructValue<T>(byte[] bytes, ref int index) where T : struct
{
Value<T>(bytes, ref index);
}
/// <summary>
/
// 解析基础值类型数组
/// </summary>
/// <typeparam name="T">基础类型</typeparam>
/// <param name="bytes">字节流数组</param>
/// <param name="index">开始Index</param>
/// <param name="len">数组长度</param>
/// <returns></returns>
public T[] getStructValus<T>(byte[] bytes, ref int index, int len) where T : struct
{
Values<T>(bytes, ref index, len);
}
/
// <summary>
/// 基础值类型2维数组解析
/// </summary>
/// <typeparam name="T">基础类型</typeparam>
/// <param name="bytes">字节流数组</param>
/// <param name="index">开始Index</param>
/// <param name="lenD1">⼀维Length</param>
/// <param name="lenD2">⼆维Length</param>
/// <returns></returns>
public T[,] getStructValus2D<T>(byte[] bytes, ref int index, int lenD1, int lenD2) where T : struct
{
Values2D<T>(bytes, ref index, lenD1, lenD2);
}
#region
/// <summary>
/// 基础值类型解析
/// </summary>
/// <typeparam name="T">值类型</typeparam>
/// <param name="bytes">字节流数组</param>
/// <param name="index">开始Index</param>
/// <returns></returns>
private T getValue<T>(byte[] bytes, ref int index) where T : struct
{
T bc = default(T);
switch (typeof(T).Name.ToLower())
{
case "uint16":
{
bc = (T)Convert.ChangeType( BitConverter.ToUInt16(bytes, index), typeof(T));                    index += Marshal.SizeOf(typeof(T));
}
break;
case "int16":
{
bc = (T)Convert.ChangeType(BitConverter.ToInt16(bytes, index), typeof(T));
index += Marshal.SizeOf(typeof(T));
}
break;
case "bool":
case "boolean":
{
bc = (T)Convert.ChangeType(BitConverter.ToBoolean(bytes, index), typeof(T));                    index += Marshal.SizeOf(typeof(T));
}
break;
case "int64":
{
bc = (T)Convert.ChangeType(BitConverter.ToInt64(bytes, index), typeof(T));
index += Marshal.SizeOf(typeof(T));
}
break;
case "uint64":
{
bc = (T)Convert.ChangeType(BitConverter.ToUInt64(bytes, index), typeof(T));                    index += Marshal.SizeOf(typeof(T));
}
break;
case "byte":
{
bc = (T)Convert.ChangeType( bytes[index], typeof(T));
index += Marshal.SizeOf(typeof(T));
}
break;
case "int32":
{
bc = (T)Convert.ChangeType(BitConverter.ToInt32(bytes, index), typeof(T));
index += Marshal.SizeOf(typeof(T));
}
break;
case "uint32":
{
bc = (T)Convert.ChangeType(BitConverter.ToUInt32(bytes, index), typeof(T));                    index += Marshal.SizeOf(typeof(T));
}
break;
}
return bc;
}
/// <summary>
/// 基础值类型数组解析
/// </summary>
/// <typeparam name="T">基础类型</typeparam>
/// <param name="bytes">字节流数组</param>
/// <param name="index">开始Index</param>
/// <param name="len">数组长度</param>
/// <returns></returns>
private T[] getValues<T>(byte[] bytes, ref int index, int len) where T : struct
{
T[] bc = new T[len];
int i = 0;
switch (typeof(T).Name.ToLower())
{
case "uint16":
{
while (i < len)
{
bc[i] = (T) Convert.ChangeType(BitConverter.ToUInt16(bytes, index), typeof(T));                            index += Marshal.SizeOf(typeof(T));
i += 1;
}
}
break;
case "int16":
{
while (i < len)
{
bc[i] = (T)Convert.ChangeType(BitConverter.ToInt16(bytes, index), typeof(T));
index += Marshal.SizeOf(typeof(T));
i += 1;
}
}
break;
case "bool":
case "boolean":
{
while (i < len)
{
bc[i] = (T)Convert.ChangeType(BitConverter.ToBoolean(bytes, index), typeof(T));                            index += Marshal.SizeOf(typeof(T));
i += 1;
}
}
break;
case "int64":
{
while (i < len)
{
bc[i] = (T)Convert.ChangeType(BitConverter.ToInt64(bytes, index), typeof(T));
index += Marshal.SizeOf(typeof(T));
i += 1;
}
}
break;
case "uint64":
{
while (i < len)
{
bc[i] = (T)Convert.ChangeType(BitConverter.ToUInt64(bytes, index), typeof(T));                            index += Marshal.SizeOf(typeof(T));
i += 1;
}
}
break;
case "byte":
{
while (i < len)
{
bc[i] = (T)Convert.ChangeType(bytes[index], typeof(T));
index += Marshal.SizeOf(typeof(T));
i += 1;
}
}
break;
case "int32":
{
while (i < len)
{
bc[i] = (T)Convert.ChangeType(BitConverter.ToInt32(bytes, index), typeof(T));
index += Marshal.SizeOf(typeof(T));
i += 1;
}
}
break;
case "uint32":
{
while (i < len)
{
bc[i] = (T)Convert.ChangeType(BitConverter.ToUInt32(bytes, index), typeof(T));
index += Marshal.SizeOf(typeof(T));
i += 1;
}
}
break;
case "char":
{
Buffer.BlockCopy( bytes, index, bc, 0,len );
index += Marshal.SizeOf(bc);
}
break;
}
return bc;
}
/// <summary>
/
// 基础值类型2维数组解析
/// </summary>
/// <typeparam name="T">基础类型</typeparam>
/// <param name="bytes">字节流数组</param>
/// <param name="index">开始Index</param>
/// <param name="lenD1">⼀维Length</param>
/// <param name="lenD2">⼆维Length</param>
/// <returns></returns>
private T[,] getValues2D<T>(byte[] bytes, ref int index, int lenD1, int lenD2) where T : struct
{
T[,] bc = new T[lenD1,lenD2];
int i = 0;
switch (typeof(T).Name.ToLower())
{
case "uint16":
{
while (i < lenD1)
{
for (int j = 0; j < lenD2; j+=1)
{
bc[i, j] = (T)Convert.ChangeType(BitConverter.ToUInt16(bytes, index), typeof(T));                                index += Marshal.SizeOf(typeof(T));
}
i += 1;
}
}
break;
case "int16":
{
while (i < lenD1)
{
for (int j = 0; j < lenD2; j += 1)
{
bc[i, j] = (T)Convert.ChangeType(BitConverter.ToInt16(bytes, index), typeof(T));
index += Marshal.SizeOf(typeof(T));
}
i += 1;
}
}
break;
case "bool":
case "boolean":
{
while (i < lenD1)
{
for (int j = 0; j < lenD2; j += 1)
{
bc[i, j] = (T)Convert.ChangeType(BitConverter.ToBoolean(bytes, index), typeof(T));                                index += Marshal.SizeOf(typeof(T));
}
i += 1;
}
}
break;
case "int64":
{
while (i < lenD1)
{
for (int j = 0; j < lenD2; j += 1)
{
bc[i, j] = (T)Convert.ChangeType(BitConverter.ToInt64(bytes, index), typeof(T));
index += Marshal.SizeOf(typeof(T));
}
i += 1;
}
}
break;
case "uint64":
{
while (i < lenD1)
{
for (int j = 0; j < lenD2; j += 1)
{
bc[i, j] = (T)Convert.ChangeType(BitConverter.ToUInt64(bytes, index), typeof(T));                                index += Marshal.SizeOf(typeof(T));
}
i += 1;
}
}
break;
case "byte":
{
while (i < lenD1)
{
for (int j = 0; j < lenD2; j += 1)
{
bc[i, j] = (T)Convert.ChangeType(bytes[index], typeof(T));
index += Marshal.SizeOf(typeof(T));
}
i += 1;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。