using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StandardLibrary
{
///
/// 数据类型转换专用类
///
static class TypeConversion
{
///
/// 将16位整形转换成字节数组
///
/// 16位整形变量
///
public static byte[] Short_Bytes(short[] value)
{
List bytes = new List();
foreach (short I16 in value)
bytes.AddRange(BitConverter.GetBytes(I16));
byte[] Return = new byte[bytes.Count];
bytes.CopyTo(Return, 0);
return Return;
}
///
/// 将32位整形转换成字节数组,可传递任意数量的实参
///
/// 32位整形变量
///
public static byte[] Int_Bytes(params int[] value)
{
List bytes = new List(256);
foreach (int I32 in value) bytes.AddRange(BitConverter.GetBytes(I32));
byte[] Return = new byte[bytes.Count];
bytes.CopyTo(Return, 0);
return Return;
}
///
/// 将64位整形转换成字节数组,可传递任意数量的实参
///
/// 64位整形变量
///
public static byte[] Long_Bytes(params long[] value)
{
List bytes = new List(256);
foreach (long I64 in value)
bytes.AddRange(BitConverter.GetBytes(I64));
byte[] Return = new byte[bytes.Count];
bytes.CopyTo(Return, 0);
return Return;
}
///
/// 将字节数组(2个元素)转换成16位有符号整形
///
/// 字节数组,低字节在前
///
public static short Bytes_Short(byte[] value)
{
if (value == null || value.Length != 2) throw new IndexOutOfRangeException("数组元素大小必须等于2");
return BitConverter.ToInt16(value, 0);
}
///
/// 将2个字节转换成16位有符号整形
///
/// 低字节
/// 高字节
///
public static short Bytes_Short(byte lowbyte, byte highbyte)
{
byte[] value = new byte[2] { lowbyte, highbyte };
return BitConverter.ToInt16(value, 0);
}
///
/// 将字节数组(4个元素)转换成32位有符号整形
///
/// 字节数组,低字节在前
///
public static int Bytes_Int(byte[] value)
{
if (value == null || value.Length != 4) throw new IndexOutOfRangeException("数组元素大小必须等于4");
return BitConverter.ToInt32(value, 0);
}
///
/// 将4个字节转换成32位有符号整形
///
/// 低字节
/// 第二字节
/// 第三字节
/// 高字节
///
public static int Bytes_Int(byte lowbyte, byte secondbyte, byte thirdbyte, byte highbyte)
{
byte[] value = new byte[4] { lowbyte, secondbyte, thirdbyte, highbyte };
return BitConverter.ToInt32(value, 0);
}
///
/// 将字节数组(8个元素)转换成64位有符号整形
///
/// 字节数组,低字节在前
///
public static long Bytes_Long(byte[] value)
{
if (value == null || value.Length != 8) throw new IndexOutOfRangeException("数组元素大小必须等于8");
return BitConverter.ToInt64(value, 0);
}
///
/// 将8个字节转换成64位有符号整形
///
/// 低字节
/// 第二字节
/// 第三字节
/// 第四字节
/// 第五字节
/// 第六字节
/// 第七字节
/// 高字节
///
public static long Bytes_Long(byte lowbyte, byte second, byte third, byte fourth, byte fifth, byte sixth, byte seventh, byte highbyte)
{
byte[] value = new byte[8] { lowbyte, second, third, fourth, fifth, sixth, seventh, highbyte };
return BitConverter.ToInt64(value, 0);
}
///
/// 将32位浮点数转换成字节数组,可传递任意数量的实参
///
/// 32位浮点数变量
///
public static byte[] Float_Bytes(params float[] value)
{
List bytes = new List(256);
foreach (float F32 in value) bytes.AddRange(BitConverter.GetBytes(F32));
byte[] Return = new byte[bytes.Count];
bytes.CopyTo(Return, 0);
return Return;
}
///
/// 将字节数组(4个元素)转换成32位浮点数
///
/// 字节数组,低字节在前
///
public static float Bytes_Float(byte[] value)
{
if (value == null || value.Length != 4) throw new IndexOutOfRangeException("数组元素大小必须等于4");
return BitConverter.ToSingle(value, 0);
}
///
/// 将64位浮点数转换成字节数组,可传递任意数量的实参
///
/// 64位浮点数变量
///
public static byte[] Double_Bytes(params double[] value)
{
List bytes = new List(256);
foreach (double F64 in value) bytes.AddRange(BitConverter.GetBytes(F64));
byte[] Return = new byte[bytes.Count];
bytes.CopyTo(Return, 0);
return Return;
}
///
/// 将字节数组(8个元素)转换成64位浮点数
///
/// 字节数组,低字节在前
///
public static double Bytes_Double(byte[] value)
{
if (value == null || value.Length != 8) throw new IndexOutOfRangeException("数组元素大小必须等于8");
return BitConverter.ToDouble(value, 0);
}
///
/// 将bool数组(8个元素)转换成字节型
///
/// bool数组
///
public static byte Bools_Byte(bool[] value)
{
if (value.Length != 8) throw new IndexOutOfRangeException("数组元素大小必须等于8");
byte Return = 0;
foreach (bool a in value)
{
Return <<= 1;
Return = a ? Return |= 1 : Return |= 0;
}
return Return;
}
///
/// 将bool数组(16个元素)转换成16位整形
///
/// bool数组
///
public static short Bools_Short(bool[] value)
{
if (value.Length != 16) throw new IndexOutOfRangeException("数组元素大小必须等于16");
short Return = 0;
foreach (bool a in value)
{
Return <<= 1;
Return = a ? Return |= 1 : Return |= 0;
}
return Return;
}
///
/// 将bool数组(32个元素)转换成32位整形
///
/// bool数组
///
public static int Bools_Int(bool[] value)
{
if (value.Length != 32) throw new IndexOutOfRangeException("数组元素大小必须等于32");
int Return = 0;
foreach (bool a in value)
{
Return <<= 1;
Return = a ? Return |= 1 : Return |= 0;
}
return Return;
}
///
/// 将字节变量转换成bool数组
///
/// 字节变量
///
public static bool[] Byte_Bools(byte value)
{
bool[] Return = new bool[8];
for (int i = 0; i < 8; i++)
{
byte transit = value;
Return[i] = (transit &= 1) == 1 ? true : false;
value >>= 1;
}
return Return;
}
///
/// 将16位无符号整形变量转换成bool数组
///
/// 16位无符号整形变量
///
public static bool[] Short_Bools(short value)
{
bool[] Return = new bool[16];
for (int i = 0; i < 16; i++)
{
short transit = value;
Return[i] = (transit &= 1) == 1 ? true : false;
value >>= 1;
}
return Return;
}
///
/// 将32位无符号整形变量转换成bool数组
///
/// 32位无符号整形变量
///
public static bool[] Int_Bools(int value)
{
bool[] Return = new bool[32];
for (int i = 0; i < 32; i++)
{
int transit = value;
Return[i] = (transit &= 1) == 1 ? true : false;
value >>= 1;
}
return Return;
}
///
/// 将任意长度的字符串转换成字节数组
///
/// 字符串
///
public static byte[] Str_Bytes(string value)
{
if (value == null) throw new ArgumentNullException("字符串不能为空");
return Encoding.UTF8.GetBytes(value);
}
///
/// 将单字符串转换成单字节
///
/// 字符串
///
public static byte Str_Byte(string value)
{
if (value == null && value.Length == 1) throw new ArgumentNullException("字符串不能为空且长度必须为1");
return Encoding.UTF8.GetBytes(value)[0];
}
///
/// 将任意长度的字节数组转换成字符串
///
/// 字节数组
///
public static string Bytes_Str(byte[] value)
{
if (value == null) throw new ArgumentNullException("数组不能为空");
return Encoding.UTF8.GetString(value);
}
///
/// 将16为整形变量转换成字符串
///
/// 16位整形变量数组
///
public static string Shorts_Str(short[] value)
{
return Bytes_Str(Short_Bytes(value));
}
///
/// 将单个bool变量转换成单个字符
///
/// bool变量
/// 单字符
public static string Bool_Str(bool value)
{
if (value) return "1";
else return "0";
}
///
/// 将bool数组转换成字符串
///
/// bool数组
/// 字符串
public static string Bools_Str(bool[] value)
{
string Return = "";
foreach (bool a in value)
{
if (a) Return += "1";
else Return += "0";
}
return Return;
}
///
/// 将字符串转换成16位整形数组
///
///
///
public static short[] Str_Shorts(string value)
{
List bytes = new List();
List shorts = new List();
bytes.AddRange(Str_Bytes(value));
while (bytes.Count > 0)
{
if (bytes.Count >= 2)
{
shorts.Add(Bytes_Short(bytes[0], bytes[1]));
bytes.RemoveAt(0); bytes.RemoveAt(0);
}
else
{
shorts.Add(Bytes_Short(bytes[0], 0));
bytes.Clear();
}
}
short[] Return = new short[shorts.Count];
shorts.CopyTo(Return, 0);
return Return;
}
///
/// 将字符串转换成16进制指节数组
///
///
///
public static byte[] Str_HexBytes(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Replace(" ", ""), 16);
return returnBytes;
}
///
/// 16进制显示
///
///
///
public static string byte_HexStr(byte[] bytes)
{
string returnStr = "";
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
returnStr += bytes[i].ToString("X2");
}
}
return returnStr;
}
///
/// 判断字符串是否可以转换成无符号整数
///
///
///
public static bool JudgeInt(string value)
{
byte[] a = Str_Bytes(value);
if (a.Length > 0)
{
bool Return = true;
for (int i = 0; i < a.Length; i++)
{
if (a[i] < 48 || a[i] > 57)
{
Return = false;
break;
}
}
return Return;
}
else return false;
}
}
}