#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* 版权所有 (c) 2026 WenJY 保留所有权利。
* CLR版本:4.0.30319.42000
* 机器名称:Mr.Wen's MacBook Pro
* 命名空间:Sln.IntelliBelt.Common
* 唯一标识:20081D43-2E8D-4CC9-B60C-1563DC642FEB
*
* 创建者:WenJY
* 电子邮箱:
* 创建时间:2026-04-24 10:49:31
* 版本:V1.0.0
* 描述:
*
*--------------------------------------------------------------------
* 修改人:
* 时间:
* 修改说明:
*
* 版本:V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
using System.Text;
namespace Sln.IntelliBelt.Common;
public class StringChange
{
///
/// 将字符串强制转换成int,转换失败则返回0
///
///
///
public int ParseToInt(string str)
{
int returnInt = 0;
if (str == null || str.Trim().Length < 1)
{
return returnInt;
}
if (int.TryParse(str, out returnInt))
{
return returnInt;
}
else
{
return 0;
}
}
///
/// char数组转Array
///
///
///
///
public string CharArrayToString(char[] cha, int len)
{
string str = "";
for (int i = 0; i < len; i++)
{
str += string.Format("{0}", cha[i]);
}
return str;
}
public byte[] HexStrTorbytes(string strHex)//e.g. " 01 01" ---> { 0x01, 0x01}
{
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 string StringToHexString(string s, Encoding encode)
{
byte[] b = encode.GetBytes(s); //按照指定编码将string编程字节数组
string result = string.Empty;
for (int i = 0; i < b.Length; i++) //逐字节变为16进制字符,以%隔开
{
result += "%" + Convert.ToString(b[i], 16);
}
return result;
}
public string HexStringToString(string hs, Encoding encode)
{
//以%分割字符串,并去掉空字符
string[] chars = hs.Split(new char[] { '%' }, StringSplitOptions.RemoveEmptyEntries);
byte[] b = new byte[chars.Length];
//逐个字符变为16进制字节数据
for (int i = 0; i < chars.Length; i++)
{
b[i] = Convert.ToByte(chars[i], 16);
}
//按照指定编码将字节数组变为字符串
return encode.GetString(b);
}
public byte[] Swap16Bytes(byte[] OldU16)
{
byte[] ReturnBytes = new byte[2];
ReturnBytes[1] = OldU16[0];
ReturnBytes[0] = OldU16[1];
return ReturnBytes;
}
///
/// 获取时间戳
///
///
public long GetTimeStamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds);
}
public byte[] ConvertFloatToINt(byte[] floatBytes)
{
byte[] intBytes = new byte[floatBytes.Length / 2];
for (int i = 0; i < intBytes.Length; i++)
{
intBytes[i] = floatBytes[i * 2];
}
return intBytes;
}
//CRC异或校验
public byte CalculateVerify(byte[] pMessage, int iLength)
{
UInt16 i;
byte iVerify = 0;
iVerify = pMessage[0];
for (i = 1; i < iLength; i++)
{
iVerify = (byte)(iVerify ^ pMessage[i]);
}
return iVerify;
}
public int HexStringToNegative(string strNumber)
{
int iNegate = 0;
int iNumber = Convert.ToInt32(strNumber, 16);
if (iNumber > 127)
{
int iComplement = iNumber - 1;
string strNegate = string.Empty;
char[] binchar = Convert.ToString(iComplement, 2).PadLeft(8, '0').ToArray();
foreach (char ch in binchar)
{
if (Convert.ToInt32(ch) == 48)
{
strNegate += "1";
}
else
{
strNegate += "0";
}
}
iNegate = -Convert.ToInt32(strNegate, 2);
}
return iNegate;
}
///
/// Byte[] 转 uint16
///
///
///
///
public void ConvertBytesToUInt16(byte[] buffer,out uint falg)
{
if (buffer == null || buffer.Length < 2)
{
throw new ArgumentException("Input array length must be at least 2.");
}
var input = buffer.Reverse().ToArray();
falg = (uint) ((input[1] << 8) | input[0]);
}
///
/// Byte[] 移位转换
///
///
///
public void SwapBytes(ref byte[] input)
{
if (input == null || input.Length % 2 != 0)
{
throw new ArgumentException("Input array length must be a multiple of 2.");
}
byte[] result = new byte[input.Length];
for (int j = 0; j < input.Length; j += 2)
{
ushort swapped = (ushort)((input[j + 1] << 8) | input[j]);
result[j] = (byte)(swapped >> 8);
result[j + 1] = (byte)swapped;
}
input = result;
}
///
/// Byte[] 转string
///
///
///
public string ConverToString(byte[] data)
{
string str;
StringBuilder stb = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
if ((int)data[i] > 15)
{
stb.Append(Convert.ToString(data[i], 16).ToUpper()); //添加字符串
}
else //如果是小于0F需要加个零
{
stb.Append("0" + Convert.ToString(data[i], 16).ToUpper());
}
}
str = stb.ToString();
return str;
}
///
/// Byte[] 转 Hex
///
///
///
///
public string bytesToHexStr(byte[] bytes, int iLen)
{
StringBuilder sb = new StringBuilder();
if (bytes != null)
{
for (int i = 0; i < iLen; i++)
{
sb.Append(bytes[i].ToString("X2"));
}
}
return sb.ToString();
}
///
/// 校验计算
///
///
///
///
public byte[] CalculateVerifyToArray(byte[] pMessage, int iLength)
{
UInt16 i;
int iVerify = 0;
iVerify = pMessage[0];
for (i = 0; i < iLength - 1; i++)
{
iVerify = iVerify + pMessage[i + 1];
}
return BitConverter.GetBytes(Convert.ToUInt16(iVerify));
}
public string TransformString(string input)
{
if (string.IsNullOrEmpty(input))
return input;
// 先将 E 替换为 9,再移除下划线
return input.Replace('E', '9').Replace("_", "");
}
public byte CalculateChecksum(byte[] data)
{
if (data == null || data.Length == 0)
return 0;
int sum = 0;
foreach (byte b in data)
{
sum += b;
}
return (byte)(sum & 0xFF); // 取低8位
}
///
/// 将字节数组按 ASCII 编码转换为字符串,遇到第一个 0x00 字节时截断。
///
/// 要转换的字节数组
/// 转换后的字符串,不含末尾的空字符
public string ToAsciiString(byte[] bytes)
{
if (bytes == null) return null;
// 查找第一个空字节的位置(若不存在则使用整个数组长度)
int length = Array.IndexOf(bytes, (byte)0);
if (length == -1) length = bytes.Length;
// 只取有效部分进行 ASCII 解码
return Encoding.ASCII.GetString(bytes, 0, length);
}
///
/// 计算字节数组的异或(XOR)校验和。
///
/// 待校验的字节数组
/// XOR 校验结果(单字节)
public static byte XorChecksum(byte[] data)
{
if (data == null || data.Length == 0)
{
return 0;
}
byte checksum = 0;
foreach (byte b in data)
{
checksum ^= b;
}
return checksum;
}
///
/// 将 16 位整数转换为字节数组(大端序,高位在前)
///
public static byte[] ToBigEndianBytes(short value)
{
byte[] bytes = BitConverter.GetBytes(value);
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes);
return bytes;
}
}