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.

125 lines
4.5 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 BatCharging.Service
{
public class MsgBits
{
/// <summary>
/// 将一个字节转换到二进制字节数组.(高位在位数组低索引位,低位在位数组高索引位)
/// </summary>
/// <param name="num">需转换字节</param>
/// <returns>二进制字节数组</returns>
private static byte[] ToFullBinaryBytes(byte num)
{
byte[] rst = new byte[8];
for (int i = 0; i < 8; i++)
{
rst[i] = (byte)((num >> i & 1));
}
return rst;
}
/// <summary>
/// 得到字节中单Bit的值(高字节在数组低索引位,低字节在数组高索引位)
/// </summary>
/// <param name="data">字节数组</param>
/// <param name="byteAddr">字节位置:数组中第几个字节(从0开始)</param>
/// <param name="bitStartAddr">Bit在字节中的位置:第几位(从0开始)</param>
/// <returns>返回字节结果byte:1,0</returns>
public static byte ByteToBitValue(byte[] data, int byteAddr, int bitStartAddr)
{
byte result = 9;
if (data.Count() >= (byteAddr + 1))
{
byte value = data[byteAddr];
result = ToFullBinaryBytes(value)[bitStartAddr];
}
return result;
}
/// <summary>
/// 得到字节中Bit组合的值,高位在前,低位在后(高字节在数组低索引位,低字节在数组高索引位)
/// </summary>
/// <param name="data">字节数组</param>
/// <param name="byteAddr">字节位置:数组中第几个字节(从0开始)</param>
/// <param name="bitStartAddr">Bit在字节中的位置:第几位(从0开始)</param>
/// <param name="bitOffset">Bit在字节中的位数</param>
/// <returns>返回字节结果byte:1,0</returns>
public static byte ByteToBitsValue(byte[] data, int byteAddr, int bitStartAddr, int bitOffset)
{
byte result = 9;
if (data.Count() >= (byteAddr + 1))
{
byte value = data[byteAddr];
if ((bitStartAddr + bitOffset) <= 8)
{
byte temp = 0;
for (int i = bitStartAddr; i < bitStartAddr + bitOffset; i++)
{
temp += (byte)(((value >> i & 1)) << (i - bitStartAddr));
}
result = temp;
}
}
return result;
}
/// <summary>
/// 根据高位优先的原理的两字节数据解析返回一个16位无符号整数(高字节在数组低索引位,低字节在数组高索引位)
/// </summary>
/// <param name="value">原字节数组</param>
/// <param name="offset">缓冲区偏移</param>
/// <returns>返回无符号整数</returns>
public static ushort ToUInt16(byte[] value, int offset)
{
return (ushort)((value[offset] << 8) + value[offset + 1]);
}
/// <summary>
/// 将字符串转换成字节数组(主要应用于车牌)
/// </summary>
/// <param name="StrSrc">字符串</param>
/// <returns>字节数组</returns>
public static byte[] StrToBytes(string StrSrc)
{
byte[] byteResults = null;
List<byte> lstBytes = null;
if (StrSrc != null)
{
if (StrSrc.Trim() != "")
{
lstBytes = new List<byte>();
int iLength = StrSrc.Length;
for (int i = 0; i < iLength; i++)
{
char chr = Convert.ToChar(StrSrc.Substring(i, 1));
int iChr = (int)chr;
if (iChr >= 256)
{
byte b1 = Convert.ToByte(iChr >> 8);
lstBytes.Add(b1);
byte b2 = Convert.ToByte(iChr % 256);
lstBytes.Add(b2);
}
else
{
lstBytes.Add(Convert.ToByte(iChr));
}
}
byteResults = lstBytes.ToArray();
}
}
return byteResults;
}
}
}