You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

300 lines
10 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HybirdFrameworkServices
{
public class ByteUtils
{
/// <summary>
/// 十六进制字符串转字符串
/// </summary>
/// <param name="strHex"></param>
/// <returns></returns>
public static string StrHex2Str(string strHex)
{
byte[] keyWord = new byte[strHex.Length / 2];
string strResult = "";
for (int i = 0; i < keyWord.Length; i++)
{
try
{
keyWord[i] = (byte)(0xff & Convert.ToInt32(strHex.Substring(i * 2, 2), 16));
strResult += Convert.ToChar(keyWord[i]).ToString();
}
catch (Exception e)
{
e.ToString();
}
}
return strResult;
}
/// <summary>
/// 字符串转16进制字符串
/// </summary>
/// <param name="strSrc"></param>
/// <returns></returns>
public static string Str2HexStr(string strSrc)
{
char[] chTemp = strSrc.ToCharArray();
string strResult = "";
foreach (var ch in chTemp)
{
int value = Convert.ToInt32(ch);
strResult += String.Format("{0:X}", value);
}
//strResult = "6000" + strResult;
return strResult;
}
/// <summary>
/// 根据高位优先的原理的两字节数据解析返回一个16为无符号整数
/// </summary>
/// <param name="value">原字节数组(含有两字节)</param>
/// <returns>返回无符号整数</returns>
public static ushort ToUInt16(byte[] value)
{
return (ushort)((value[0] << 8) | (value[1] & 0x00FF));
}
/// <summary>
/// 根据高位优先的原理的两字节数据解析返回一个16为无符号整数
/// </summary>
/// <param name="value">原字节数组</param>
/// <param name="offset">缓冲区偏移</param>
/// <returns>返回无符号整数</returns>
public static UInt16 ToUInt16(byte[] value, int offset)
{
return (ushort)((value[offset] << 8) | (value[offset + 1] & 0x00FF));
}
/// <summary>
/// 根据高位优先颠倒的原理的两字节数据解析返回一个16为无符号整数
/// </summary>
/// <param name="value">原字节数组</param>
/// <param name="offset">缓冲区偏移</param>
/// <returns>返回无符号整数</returns>
public static UInt16 DToUInt16(byte[] value, int offset)
{
return (ushort)(value[offset] + (value[offset + 1] << 8));
}
public static UInt32 ToUInt32(byte[] value, int offset)
{
UInt16 low = (ushort)((value[offset + 1] << 8) | (value[offset] & 0x00FF));
UInt16 height = (ushort)((value[offset + 3] << 8) | (value[offset + 2] & 0x00FF));
return (UInt32)(height << 16 | low);
}
public static UInt32 DToUInt32(byte[] value, int offset)
{
UInt16 low = (ushort)((value[offset + 1] << 8) | (value[offset] & 0x00FF));
UInt16 height = (ushort)((value[offset + 3] << 8) | (value[offset + 2] & 0x00FF));
return (UInt32)(height << 16 | low);
}
/// <summary>
/// 16进制数高地位对调
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static byte[] Int16ToByteArray(Int16 value) //16进制数高地位对调
{
byte[] bytesResult = new byte[2];
byte[] temp = new byte[2];
BitConverter.GetBytes(Convert.ToInt16(value)).CopyTo(temp, 0);
bytesResult[0] = temp[1];
bytesResult[1] = temp[0];
return bytesResult;
}
/// <summary>
/// 32进制数高地位对调
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static byte[] Int32ToByteArray(Int32 value) //16进制数高地位对调
{
byte[] bytesResult = new byte[4];
byte[] temp = new byte[4];
BitConverter.GetBytes(Convert.ToInt32(value)).CopyTo(temp, 0);
bytesResult[0] = temp[3];
bytesResult[1] = temp[2];
bytesResult[2] = temp[1];
bytesResult[3] = temp[0];
return bytesResult;
}
/// <summary>
/// 获取16进制字符串的字节数组
/// </summary>
/// <param name="hexString">hexString 16进制字符串</param>
/// <returns>字节数组</returns>
public static byte[] ToByteByHexStr(string hexString)
{
if (hexString == null)
return null;
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), 16);
return returnBytes;
}
/// <summary>
/// 获取32进制字符串的字节数组
/// </summary>
/// <param name="hexString">hexString 16进制字符串</param>
/// <returns>字节数组</returns>
public static byte[] ToByteByHexStr32(string hexString, int leng)
{
byte[] bytes = new byte[leng];
// 使用UTF8编码将字符串转换为字节数组
byte[] byteArray = Encoding.ASCII.GetBytes(hexString);
// 如果需要确保数组长度为32字节可以添加逻辑来处理长度不足的情况
if (byteArray.Length < leng)
{
Array.Copy(byteArray, 0, bytes, 0, byteArray.Length);
}
//else if (byteArray.Length > leng)
//{
// // 长度超过32字节可以截断或者抛出异常
// Array.Copy(byteArray, 0, new byte[leng], 0, leng); // 截断数组到32字节
//}
return bytes;
}
/// <summary>
/// 获取16进制字符串的字节数组
/// </summary>
/// <param name="hexString">hexString 16进制字符串</param>
/// <returns>字节数组</returns>
public static byte[] ToByteByStr(string strValue)
{
#region 转化到十六进制字符串
char[] chTemp = strValue.ToCharArray();
string strHex = "";
foreach (var ch in chTemp)
{
int value = Convert.ToInt32(ch);
strHex += String.Format("{0:X}", value);
}
#endregion 转化到十六进制字符串
if (strHex == null)
return null;
strHex = strHex.Replace(" ", "");
if ((strHex.Length % 2) != 0)
strHex += " ";
byte[] returnBytes = new byte[strHex.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(strHex.Substring(i * 2, 2), 16);
return returnBytes;
}
public static string BytesToHexStr(byte[] data)
{
StringBuilder sb = new StringBuilder(data.Length * 3);
foreach (byte b in data)
sb.Append(Convert.ToString(b, 16).PadLeft(2, '0') + " ");
return sb.ToString().ToUpper();
}
/// <summary>
/// 字节转换成布尔数组
/// </summary>
/// <param name="b"></param>
/// <returns></returns>
public static bool[] ByteToBoolArray(byte value)
{
bool[] array = new bool[8];
for (int i = 0; i <= 7; i++)
{
//对于byte的每bit进行判定
array[i] = (value & 1) == 1; //判定byte的最后一位是否为1若为1则是true否则是false
value = (byte)(value >> 1); //将byte右移一位
}
return array;
}
/// <summary>
/// UInt16转换成布尔数组
/// </summary>
/// <param name="value">16位无符号整形</param>
/// <returns>布尔数组</returns>
public static bool[] UInt16ToBoolArray(ushort value)
{
bool[] array = new bool[16];
for (int i = 0; i <= 15; i++)
{
//对于byte的每bit进行判定
array[i] = (value & 1) == 1; //判定byte的最后一位是否为1若为1则是true否则是false
value = (ushort)(value >> 1); //将byte右移一位
}
return array;
}
public static string CP56time2aToStr(byte[] bytes)
{
return "20" + Convert.ToInt32(bytes[6]).ToString("00") + "-" + Convert.ToInt32(bytes[5]).ToString("00")
+ "-" + Convert.ToInt32(bytes[4]).ToString("00") + " " + Convert.ToInt32(bytes[3]).ToString("00")
+ ":" + Convert.ToInt32(bytes[2]).ToString("00") + ":"
+ (ToFUInt16(bytes, 0) / 1000).ToString("00");
}
public static ushort ToFUInt16(byte[] value, int offset)
{
return (ushort)((value[offset + 1] << 8) | (value[offset] & 0x00FF));
}
/// <summary>
/// BCD码转byte
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static byte BCDToByte(byte value)
{
//高四位
byte high = (byte)((value >> 4 & 0xF));
//低四位
byte low = (byte)(value & 0xF);
return (byte)(high * 10 + low);
}
/// <summary>
/// byte转BCD码
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static byte ByteToBCD(byte value)
{
//高四位
byte high = (byte)(value / 10);
//低四位
byte low = (byte)(value % 10);
return (byte)(high << 4 | low);
}
}
}