|
|
#region << 版 本 注 释 >>
|
|
|
|
|
|
/*--------------------------------------------------------------------
|
|
|
* 版权所有 (c) 2025 WenJY 保留所有权利。
|
|
|
* CLR版本:4.0.30319.42000
|
|
|
* 机器名称:Mr.Wen's MacBook Pro
|
|
|
* 命名空间:Sln.Iot.Common
|
|
|
* 唯一标识:78FCCE90-A4C0-4DCC-AB23-7C21EC7ECE05
|
|
|
*
|
|
|
* 创建者:WenJY
|
|
|
* 电子邮箱:
|
|
|
* 创建时间:2025-04-11 15:06:46
|
|
|
* 版本:V1.0.0
|
|
|
* 描述:
|
|
|
*
|
|
|
*--------------------------------------------------------------------
|
|
|
* 修改人:
|
|
|
* 时间:
|
|
|
* 修改说明:
|
|
|
*
|
|
|
* 版本:V1.0.0
|
|
|
*--------------------------------------------------------------------*/
|
|
|
|
|
|
#endregion << 版 本 注 释 >>
|
|
|
|
|
|
using System;
|
|
|
using System.Drawing;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
|
|
|
namespace Sln.Iot.Common
|
|
|
{
|
|
|
public class StringChange
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 将字符串强制转换成int,转换失败则返回0
|
|
|
/// </summary>
|
|
|
/// <param name="str"></param>
|
|
|
/// <returns></returns>
|
|
|
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;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// char数组转Array
|
|
|
/// </summary>
|
|
|
/// <param name="cha"></param>
|
|
|
/// <param name="len"></param>
|
|
|
/// <returns></returns>
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <param name="strbase64">64Base码</param>
|
|
|
/// <param name="path">保存路径</param>
|
|
|
/// <param name="filename">文件名称</param>
|
|
|
/// <returns></returns>
|
|
|
public bool Base64ToImage(string strbase64, string path, string filename)
|
|
|
{
|
|
|
bool Flag = false;
|
|
|
try
|
|
|
{
|
|
|
//base64编码的文本 转为 图片
|
|
|
//图片名称
|
|
|
byte[] arr = Convert.FromBase64String(strbase64);//将指定的字符串(它将二进制数据编码为 Base64 数字)转换为等效的 8 位无符号整数数组。
|
|
|
using (MemoryStream ms = new MemoryStream(arr))
|
|
|
{
|
|
|
Bitmap bmp = new Bitmap(ms);//加载图像
|
|
|
if (!Directory.Exists(path))//判断保存目录是否存在
|
|
|
{
|
|
|
Directory.CreateDirectory(path);
|
|
|
}
|
|
|
bmp.Save((path + "\\" + filename + ".png"), System.Drawing.Imaging.ImageFormat.Png);//将图片以JPEG格式保存在指定目录(可以选择其他图片格式)
|
|
|
ms.Close();//关闭流并释放
|
|
|
if (File.Exists(path + "\\" + filename + ".png"))//判断是否存在
|
|
|
{
|
|
|
Flag = true;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
Console.WriteLine("图片保存失败:" + ex.Message);
|
|
|
}
|
|
|
return Flag;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取时间戳
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// Byte[] 转 uint16
|
|
|
/// </summary>
|
|
|
/// <param name="buffer"></param>
|
|
|
/// <param name="falg"></param>
|
|
|
/// <exception cref="ArgumentException"></exception>
|
|
|
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]);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// Byte[] 移位转换
|
|
|
/// </summary>
|
|
|
/// <param name="input"></param>
|
|
|
/// <exception cref="ArgumentException"></exception>
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// Byte[] 转string
|
|
|
/// </summary>
|
|
|
/// <param name="data"></param>
|
|
|
/// <returns></returns>
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// Byte[] 转 Hex
|
|
|
/// </summary>
|
|
|
/// <param name="bytes"></param>
|
|
|
/// <param name="iLen"></param>
|
|
|
/// <returns></returns>
|
|
|
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();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 校验计算
|
|
|
/// </summary>
|
|
|
/// <param name="pMessage"></param>
|
|
|
/// <param name="iLength"></param>
|
|
|
/// <returns></returns>
|
|
|
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));
|
|
|
}
|
|
|
}
|
|
|
} |