|
|
@@ -6,7 +6,7 @@ namespace YSAI.Unility
|
|
|
/// <summary>
|
|
|
/// 字节处理工具
|
|
|
/// </summary>
|
|
|
- public class ByteTool
|
|
|
+ public static class ByteTool
|
|
|
{
|
|
|
private static readonly ushort[] CrcTable =
|
|
|
{
|
|
|
@@ -358,7 +358,7 @@ namespace YSAI.Unility
|
|
|
/// </summary>
|
|
|
/// <param name="bytes"></param>
|
|
|
/// <returns></returns>
|
|
|
- public static byte[] ByteTrimEnd(byte[] bytes)
|
|
|
+ public static byte[] TrimEnd(this byte[] bytes)
|
|
|
{
|
|
|
List<byte> list = bytes.ToList();
|
|
|
for (int i = bytes.Length - 1; i >= 0; i--)
|
|
|
@@ -372,10 +372,6 @@ namespace YSAI.Unility
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
- if (list.Count.Equals(0))
|
|
|
- {
|
|
|
- list.Add(0x00);
|
|
|
- }
|
|
|
return list.ToArray();
|
|
|
}
|
|
|
|
|
|
@@ -384,27 +380,44 @@ namespace YSAI.Unility
|
|
|
/// </summary>
|
|
|
/// <param name="bytes"></param>
|
|
|
/// <returns></returns>
|
|
|
- public static string HexToStr(byte[] bytes, bool ClearEmpty = false)
|
|
|
+ public static string HexToStr(this byte[] bytes, bool ClearEmpty = false)
|
|
|
{
|
|
|
if (ClearEmpty)
|
|
|
{
|
|
|
- bytes = ByteTrimEnd(bytes);
|
|
|
+ bytes = TrimEnd(bytes);
|
|
|
}
|
|
|
- string InfoStr = string.Empty;
|
|
|
- int index = 1;
|
|
|
- foreach (var item in bytes)
|
|
|
+ return ByteArrayToHexString(bytes);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// byte[] 分割
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="Data">数据</param>
|
|
|
+ /// <param name="Segmentation">几位几位分割</param>
|
|
|
+ public static List<byte[]> ByteListSegmentation(this byte[] Data, int Segmentation)
|
|
|
+ {
|
|
|
+ List<byte[]> bytels = new List<byte[]>();
|
|
|
+ for (int i = 0; i < Data.Length; i += Segmentation)
|
|
|
{
|
|
|
- if (index.Equals(bytes.Length))
|
|
|
- {
|
|
|
- InfoStr += item.ToString("x2");
|
|
|
- }
|
|
|
- else
|
|
|
+ byte[] bytes = new byte[Segmentation];
|
|
|
+ for (int ic = 0; ic < Segmentation; ic++)
|
|
|
{
|
|
|
- InfoStr += item.ToString("x2") + " ";
|
|
|
+ bytes[ic] = Data[i + ic];
|
|
|
}
|
|
|
- ++index;
|
|
|
+ bytels.Add(bytes);
|
|
|
+ if (bytels.Count.Equals(Data.Length / Segmentation)) break;
|
|
|
}
|
|
|
- return InfoStr;
|
|
|
+ return bytels;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 将十六进制数字字符串(例如:E4 CA B2)转换为字节数组。
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="s"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static byte[]? HexStringToByteArray(string s)
|
|
|
+ {
|
|
|
+ return StrToHex(s);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -412,7 +425,7 @@ namespace YSAI.Unility
|
|
|
/// </summary>
|
|
|
/// <param name="bytes"></param>
|
|
|
/// <returns></returns>
|
|
|
- public static string HexToStr(object bytes, bool ClearEmpty = false)
|
|
|
+ public static string HexToStr(this object bytes, bool ClearEmpty = false)
|
|
|
{
|
|
|
if (bytes as byte[] != null)
|
|
|
{
|
|
|
@@ -429,11 +442,43 @@ namespace YSAI.Unility
|
|
|
/// </summary>
|
|
|
/// <param name="bytes"></param>
|
|
|
/// <returns></returns>
|
|
|
- public static string HexToStr(byte bytes)
|
|
|
+ public static string HexToStr(this byte bytes)
|
|
|
{
|
|
|
return bytes.ToString("x2");
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 16进制字符串转换
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="hexstring">用空格切割字符串</param>
|
|
|
+ /// <returns>返回一个二进制字符串</returns>
|
|
|
+ public static byte[] StrToHex(this string hexstring)
|
|
|
+ {
|
|
|
+ string[] tmpary = hexstring.Trim().Split(' ');
|
|
|
+ byte[] buff = new byte[tmpary.Length];
|
|
|
+ for (int i = 0; i < buff.Length; i++)
|
|
|
+ {
|
|
|
+ buff[i] = Convert.ToByte(tmpary[i], 16);
|
|
|
+ }
|
|
|
+ return buff;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 将字节数组转换为 EA AX 字符串
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="data"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static string ByteArrayToHexString(byte[] data)
|
|
|
+ {
|
|
|
+ StringBuilder sb = new StringBuilder(data.Length * 3);
|
|
|
+ foreach (byte b in data)
|
|
|
+ {
|
|
|
+ sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
|
|
|
+ }
|
|
|
+
|
|
|
+ return sb.ToString().ToUpper();
|
|
|
+ }
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// byte[] 字节分割
|
|
|
/// 保存预留位
|
|
|
@@ -442,7 +487,7 @@ namespace YSAI.Unility
|
|
|
/// <param name="ReserveLocation">头部预留位</param>
|
|
|
/// <param name="SplitCount">分割数 几位几位分割</param>
|
|
|
/// <returns></returns>
|
|
|
- public static List<byte[]> ByteDataSplit(byte[] FileStreamData, int ReserveLocation, int SplitCount)
|
|
|
+ public static List<byte[]> ByteDataSplit(this byte[] FileStreamData, int ReserveLocation, int SplitCount)
|
|
|
{
|
|
|
List<byte[]> ByteList = new List<byte[]>(); //存储
|
|
|
double Num = Convert.ToDouble(FileStreamData.Length) / Convert.ToDouble(SplitCount - ReserveLocation); //计算循环数
|
|
|
@@ -660,7 +705,7 @@ namespace YSAI.Unility
|
|
|
src[2] = (byte)((value >> 16) & 0xFF);
|
|
|
src[1] = (byte)((value >> 8) & 0xFF);
|
|
|
src[0] = (byte)(value & 0xFF);
|
|
|
- return ByteTrimEnd(src);
|
|
|
+ return TrimEnd(src);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -694,64 +739,6 @@ namespace YSAI.Unility
|
|
|
return src;
|
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// byte[] 分割
|
|
|
- /// </summary>
|
|
|
- /// <param name="Data">数据</param>
|
|
|
- /// <param name="Segmentation">几位几位分割</param>
|
|
|
- public static List<byte[]> ByteListSegmentation(byte[] Data, int Segmentation)
|
|
|
- {
|
|
|
- List<byte[]> bytels = new List<byte[]>();
|
|
|
- for (int i = 0; i < Data.Length; i += Segmentation)
|
|
|
- {
|
|
|
- byte[] bytes = new byte[Segmentation];
|
|
|
- for (int ic = 0; ic < Segmentation; ic++)
|
|
|
- {
|
|
|
- bytes[ic] = Data[i + ic];
|
|
|
- }
|
|
|
- bytels.Add(bytes);
|
|
|
- if (bytels.Count.Equals(Data.Length / Segmentation)) break;
|
|
|
- }
|
|
|
- return bytels;
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 将十六进制数字字符串(例如:E4 CA B2)转换为字节数组。
|
|
|
- /// </summary>
|
|
|
- /// <param name="s"></param>
|
|
|
- /// <returns></returns>
|
|
|
- public static object HexStringToByteArray(string s)
|
|
|
- {
|
|
|
- s = s.Replace(" ", "").Replace("\r", "").Replace("\n", "");
|
|
|
- byte[] buffer = new byte[s.Length / 2];
|
|
|
- for (int i = 0; i < s.Length; i += 2)
|
|
|
- {
|
|
|
- buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
|
|
|
- }
|
|
|
- if (buffer.Length > 1)
|
|
|
- {
|
|
|
- return buffer;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- return buffer[0];
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary> Converts an array of bytes into a formatted string of hex digits (ex: E4 CA B2)</summary>
|
|
|
- /// <param name="data"> The array of bytes to be translated into a string of hex digits. </param>
|
|
|
- /// <returns> Returns a well formatted string of hex digits with spacing. </returns>
|
|
|
- public static string ByteArrayToHexString(byte[] data)
|
|
|
- {
|
|
|
- StringBuilder sb = new StringBuilder(data.Length * 3);
|
|
|
- foreach (byte b in data)
|
|
|
- {
|
|
|
- sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
|
|
|
- }
|
|
|
-
|
|
|
- return sb.ToString().ToUpper();
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// 将一条十六进制字符串转换为ASCII
|
|
|
/// </summary>
|
|
|
@@ -759,7 +746,7 @@ namespace YSAI.Unility
|
|
|
/// <returns>返回一条ASCII码</returns>
|
|
|
public static string HexStringToASCII(string hexstring)
|
|
|
{
|
|
|
- byte[] bt = HexStringToBinary(hexstring);
|
|
|
+ byte[] bt = StrToHex(hexstring);
|
|
|
string lin = "";
|
|
|
for (int i = 0; i < bt.Length; i++)
|
|
|
{
|
|
|
@@ -781,22 +768,6 @@ namespace YSAI.Unility
|
|
|
|
|
|
/**/
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// 16进制字符串转换
|
|
|
- /// </summary>
|
|
|
- /// <param name="hexstring">用空格切割字符串</param>
|
|
|
- /// <returns>返回一个二进制字符串</returns>
|
|
|
- public static byte[] HexStringToBinary(string hexstring)
|
|
|
- {
|
|
|
- string[] tmpary = hexstring.Trim().Split(' ');
|
|
|
- byte[] buff = new byte[tmpary.Length];
|
|
|
- for (int i = 0; i < buff.Length; i++)
|
|
|
- {
|
|
|
- buff[i] = Convert.ToByte(tmpary[i], 16);
|
|
|
- }
|
|
|
- return buff;
|
|
|
- }
|
|
|
-
|
|
|
public static int StringToHexOrDec(string strData)
|
|
|
{
|
|
|
int dData = -1;
|
|
|
@@ -826,29 +797,12 @@ namespace YSAI.Unility
|
|
|
return dData;
|
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// 将byte型转换为字符串
|
|
|
- /// </summary>
|
|
|
- /// <param name="arrInput">byte型数组</param>
|
|
|
- /// <returns>目标字符串</returns>
|
|
|
- private string ByteArrayToString(byte[] arrInput)
|
|
|
- {
|
|
|
- int i;
|
|
|
- StringBuilder sOutput = new StringBuilder(arrInput.Length);
|
|
|
- for (i = 0; i < arrInput.Length; i++)
|
|
|
- {
|
|
|
- sOutput.Append(arrInput[i].ToString("X2"));
|
|
|
- }
|
|
|
- //将此实例的值转换为System.String
|
|
|
- return sOutput.ToString();
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// 对接收到的数据进行解包(将接收到的byte型数组解包为Unicode字符串)
|
|
|
/// </summary>
|
|
|
/// <param name="recbytes">byte型数组</param>
|
|
|
/// <returns>Unicode编码的字符串</returns>
|
|
|
- public string disPackage(byte[] recbytes)
|
|
|
+ public static string disPackage(byte[] recbytes)
|
|
|
{
|
|
|
string temp = "";
|
|
|
foreach (byte b in recbytes)
|