10 Star 96 Fork 39

电子DIY工作坊/simpleSCOM

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Transform.cs 27.37 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
/// <summary>
/// whble
/// 2019年8月11日
/// </summary>
namespace 串口助手
{
/// <summary>
/// Transform data to different type. it's useful.
/// </summary>
public static class Transform
{
#region Common
/// <summary>
///
/// </summary>
/// <param name="first"></param>
/// <param name="second"></param>
/// <returns></returns>
public static byte[] ConcatArrays(byte[] first, byte[] second)
{
byte[] ret = new byte[first.Length + second.Length];
Buffer.BlockCopy(first, 0, ret, 0, first.Length);
Buffer.BlockCopy(second, 0, ret, first.Length, second.Length);
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="first"></param>
/// <param name="second"></param>
/// <param name="third"></param>
/// <returns></returns>
public static byte[] ConcatArrays(byte[] first, byte[] second, byte[] third)
{
byte[] ret = new byte[first.Length + second.Length + third.Length];
Buffer.BlockCopy(first, 0, ret, 0, first.Length);
Buffer.BlockCopy(second, 0, ret, first.Length, second.Length);
Buffer.BlockCopy(third, 0, ret, first.Length + second.Length,
third.Length);
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="arrays"></param>
/// <returns></returns>
public static byte[] ConcatArrays(params byte[][] arrays)
{
byte[] ret = new byte[arrays.Sum(x => x.Length)];
int offset = 0;
foreach (byte[] data in arrays)
{
Buffer.BlockCopy(data, 0, ret, offset, data.Length);
offset += data.Length;
}
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="arrays"></param>
/// <returns></returns>
public static byte[] ConcatBytes(params byte[] arrays)
{
byte[] ret = new byte[arrays.Length];
for (int i = 0; i < ret.Length; i++)
{
ret[i] = arrays[i];
}
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="haystack"></param>
/// <param name="needle"></param>
/// <returns></returns>
public static int Search(byte[] haystack, byte[] needle)
{
for (int i = 0; i <= haystack.Length - needle.Length; i++)
{
if (Match(haystack, needle, i))
{
return i;
}
}
return -1;
}
/// <summary>
///
/// </summary>
/// <param name="haystack"></param>
/// <param name="needle"></param>
/// <param name="start"></param>
/// <returns></returns>
public static bool Match(byte[] haystack, byte[] needle, int start)
{
if (needle.Length + start > haystack.Length)
{
return false;
}
else
{
for (int i = 0; i < needle.Length; i++)
{
if (needle[i] != haystack[i + start])
{
return false;
}
}
return true;
}
}
/// <summary>
///
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
public static bool IsHexString(string hexString)
{
if (Regex.IsMatch(hexString.Trim(), "^[0-9A-Fa-f]+$"))
{
return true;
}
return false;
}
/// <summary>
///
/// </summary>
/// <param name="hexByte"></param>
/// <returns></returns>
private static byte HexToByte(string hexByte)
{
if (hexByte.Length > 2 || hexByte.Length <= 0)
throw new ArgumentException("hex must be 1 or 2 characters in length");
byte newByte = byte.Parse(hexByte, System.Globalization.NumberStyles.HexNumber);
return newByte;
}
#endregion
#region Get Bool or Bools from buffer
/// <summary>
/// 获取byte数组中0x00 为false ,0x01 为true
/// </summary>
/// <param name="buffer"></param>
/// <param name="index"></param>
/// <returns></returns>
public static bool ToBool(byte[] buffer, int index)
{
return ((buffer[index] & 0x01) == 0x01);
}
/// <summary>
/// 获取byte字节中指定索引的bool值,1为true 0为false
/// </summary>
/// <param name="buffer">输入数据</param>
/// <param name="index">从低位开始的索引(1-8)</param>
/// <returns></returns>
public static bool ToBool(byte buffer,int index)
{
return (buffer & (0x01 << (index-1))) == (0x01 << (index - 1)) ? true : false;
}
/// <summary>
/// 获取byte字节bool值1为true 0为false
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static bool[] ToBools(byte buffer)
{
bool[] boolArray = new bool[8];
for(int i=0;i<8;i++)
{
boolArray[i] = (buffer & (0x01 << i)) == (0x01 << i) ? true : false;
}
return boolArray;
}
/// <summary>
/// 将字节数组中指定索引和指定字节长度数据转换成Bool[]
/// </summary>
/// <param name="buffer">输入数据</param>
/// <param name="index">索引</param>
/// <param name="length">长度</param>
/// <returns></returns>
public static bool[] ToBools(byte[] buffer, int index, int length)
{
byte[] tmp = new byte[length];
Array.Copy(buffer, index, tmp, 0, length);
return ToBools(tmp, length * 8);
}
/// <summary>
/// 将字节数组转成bool数组
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static bool[] ToBools(byte[] buffer)
{
if (buffer == null) return null;
return ToBools(buffer, buffer.Length * 8);
}
/// <summary>
/// 将字节数组转成bool数组
/// </summary>
/// <param name="buffer"></param>
/// <param name="length">要获取的位数</param>
/// <returns></returns>
public static bool[] ToBools(byte[] buffer, int length)
{
if (buffer == null) return null;
if (length > buffer.Length * 8) length = buffer.Length * 8;
bool[] blist = new bool[length];
for (int i = 0; i < length; i++)
{
int index = i / 8;
int offect = i % 8;
byte temp = 0;
switch (offect)
{
case 0: temp = 0x01; break;
case 1: temp = 0x02; break;
case 2: temp = 0x04; break;
case 3: temp = 0x08; break;
case 4: temp = 0x10; break;
case 5: temp = 0x20; break;
case 6: temp = 0x40; break;
case 7: temp = 0x80; break;
default: break;
}
if ((buffer[index] & temp) == temp)
{
blist[i] = true;
}
}
return blist;
}
#endregion
#region Get Byte or Bytes from buffer
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <param name="index"></param>
/// <returns></returns>
public static byte ToByte(byte[] buffer, int index)
{
return buffer[index];
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static byte[] ToBytes(bool buffer)
{
return ToBytes(new bool[] { buffer });
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static byte[] ToBytes(bool[] buffer)
{
if (buffer == null) return null;
int length = buffer.Length % 8 == 0 ? buffer.Length / 8 : buffer.Length / 8 + 1;
byte[] bytelist = new byte[length];
for (int i = 0; i < buffer.Length; i++)
{
int index = i / 8;
int offect = i % 8;
byte temp = 0;
switch (offect)
{
case 0: temp = 0x01; break;
case 1: temp = 0x02; break;
case 2: temp = 0x04; break;
case 3: temp = 0x08; break;
case 4: temp = 0x10; break;
case 5: temp = 0x20; break;
case 6: temp = 0x40; break;
case 7: temp = 0x80; break;
default: break;
}
if (buffer[i]) bytelist[index] += temp;
}
return bytelist;
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static byte[] ToBytes(byte buffer)
{
return new byte[] { buffer };
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <param name="index"></param>
/// <param name="length"></param>
/// <returns></returns>
public static byte[] ToBytes(byte[] buffer, int index, int length)
{
byte[] tmp = new byte[length];
Array.Copy(buffer, index, tmp, 0, length);
return tmp;
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static byte[] ToBytes(short buffer)
{
return ToBytes(new short[] { buffer });
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static byte[] ToBytes(short[] buffer)
{
if (buffer == null) return null;
byte[] blist = new byte[buffer.Length * 2];
for (int i = 0; i < buffer.Length; i++)
{
BitConverter.GetBytes(buffer[i]).CopyTo(blist, 2 * i);
}
return blist;
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static byte[] ToBytes(ushort buffer)
{
return ToBytes(new ushort[] { buffer });
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static byte[] ToBytes(ushort[] buffer)
{
if (buffer == null) return null;
byte[] blist = new byte[buffer.Length * 2];
for (int i = 0; i < buffer.Length; i++)
{
BitConverter.GetBytes(buffer[i]).CopyTo(blist, 2 * i);
}
return blist;
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static byte[] ToBytes(int buffer)
{
return ToBytes(new int[] { buffer });
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static byte[] ToBytes(int[] buffer)
{
if (buffer == null) return null;
byte[] blist = new byte[buffer.Length * 4];
for (int i = 0; i < buffer.Length; i++)
{
BitConverter.GetBytes(buffer[i]).CopyTo(blist, 4 * i);
}
return blist;
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static byte[] ToBytes(uint buffer)
{
return ToBytes(new uint[] { buffer });
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static byte[] ToBytes(uint[] buffer)
{
if (buffer == null) return null;
byte[] blist = new byte[buffer.Length * 4];
for (int i = 0; i < buffer.Length; i++)
{
BitConverter.GetBytes(buffer[i]).CopyTo(blist, 4 * i);
}
return blist;
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static byte[] ToBytes(float buffer)
{
return ToBytes(new float[] { buffer });
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static byte[] ToBytes(float[] buffer)
{
if (buffer == null) return null;
byte[] blist = new byte[buffer.Length * 4];
for (int i = 0; i < buffer.Length; i++)
{
BitConverter.GetBytes(buffer[i]).CopyTo(blist, 4 * i);
}
return blist;
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static byte[] ToBytes(long buffer)
{
return ToBytes(new long[] { buffer });
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static byte[] ToBytes(long[] buffer)
{
if (buffer == null) return null;
byte[] blist = new byte[buffer.Length * 8];
for (int i = 0; i < buffer.Length; i++)
{
BitConverter.GetBytes(buffer[i]).CopyTo(blist, 8 * i);
}
return blist;
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static byte[] ToBytes(ulong buffer)
{
return ToBytes(new ulong[] { buffer });
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static byte[] ToBytes(ulong[] buffer)
{
if (buffer == null) return null;
byte[] blist = new byte[buffer.Length * 8];
for (int i = 0; i < buffer.Length; i++)
{
BitConverter.GetBytes(buffer[i]).CopyTo(blist, 8 * i);
}
return blist;
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static byte[] ToBytes(double buffer)
{
return ToBytes(new double[] { buffer });
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static byte[] ToBytes(double[] buffer)
{
if (buffer == null) return null;
byte[] blist = new byte[buffer.Length * 8];
for (int i = 0; i < buffer.Length; i++)
{
BitConverter.GetBytes(buffer[i]).CopyTo(blist, 8 * i);
}
return blist;
}
/// <summary>
///
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
public static byte[] ToBytes(string hexString)
{
if (hexString == null)
throw new ArgumentNullException("hex");
if (hexString.Length % 2 != 0)
throw new FormatException("Hex string must have even number of characters.");
byte[] bytes = new byte[hexString.Trim().Length / 2];
for (int i = 0; i < bytes.Length; i++)
bytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return bytes;
}
#endregion
#region Get Short,UShort,Shorts,UShort from buffer
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <param name="index"></param>
/// <returns></returns>
public static short ToInt16(byte[] buffer, int index)
{
return BitConverter.ToInt16(buffer, index);
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <param name="index"></param>
/// <param name="length"></param>
/// <returns></returns>
public static short[] ToInt16s(byte[] buffer, int index, int length)
{
short[] tmp = new short[length];
for (int i = 0; i < length; i++)
{
tmp[i] = ToInt16(buffer, index + 2 * i);
}
return tmp;
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <param name="index"></param>
/// <returns></returns>
public static ushort ToUInt16(byte[] buffer, int index)
{
return BitConverter.ToUInt16(buffer, index);
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <param name="index"></param>
/// <param name="length"></param>
/// <returns></returns>
public static ushort[] ToUInt16s(byte[] buffer, int index, int length)
{
ushort[] tmp = new ushort[length];
for (int i = 0; i < length; i++)
{
tmp[i] = ToUInt16(buffer, index + 2 * i);
}
return tmp;
}
#endregion
#region Get Int,UInt,Ints,UInts from buffer
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <param name="index"></param>
/// <returns></returns>
public static int ToInt32(byte[] buffer, int index)
{
return BitConverter.ToInt32(buffer, index);
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <param name="index"></param>
/// <param name="length"></param>
/// <returns></returns>
public static int[] ToInt32s(byte[] buffer, int index, int length)
{
int[] tmp = new int[length];
for (int i = 0; i < length; i++)
{
tmp[i] = ToInt32(buffer, index + 2 * i);
}
return tmp;
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <param name="index"></param>
/// <returns></returns>
public static uint ToUInt32(byte[] buffer, int index)
{
return BitConverter.ToUInt32(buffer, index);
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <param name="index"></param>
/// <param name="length"></param>
/// <returns></returns>
public static uint[] ToUInt32s(byte[] buffer, int index, int length)
{
uint[] tmp = new uint[length];
for (int i = 0; i < length; i++)
{
tmp[i] = ToUInt32(buffer, index + 2 * i);
}
return tmp;
}
#endregion
#region Get HexString from buffer
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static string ToHexString(byte[] buffer)
{
return ToHexString( buffer, 0, "");
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <param name="split"></param>
/// <returns></returns>
public static string ToHexString(byte[] buffer,string split)
{
return ToHexString(buffer, 0, split);
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <param name="index"></param>
/// <param name="split"></param>
/// <returns></returns>
public static string ToHexString(byte[] buffer, int index, string split)
{
var str = new StringBuilder();
int start = Environment.TickCount;
Console.WriteLine("wait...");
for (int i = 0; i < buffer.Length; i++)
{
str.Append(String.Format("{0:X2}", buffer[i]));
str.Append(split);
}
string s = str.ToString();
Console.WriteLine(Math.Abs(Environment.TickCount - start));
return s;
//Console.WriteLine("wait...");
//if (buffer == null) return null;
//string hexString = "";
//for (int i = 0; i < buffer.Length; i++)
//{
// hexString += buffer[i].ToString("X2") + split;
//}
//Console.WriteLine(Math.Abs(Environment.TickCount - start));
//return hexString;
}
#endregion
#region Get Single,Singles from buffer
/// <summary>
/// Default change little-endian to single
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static float ToSingle(byte[] buffer)
{
if(buffer.Length<sizeof(float))
throw new FormatException(string.Format("buffer does not contain enough bytes. Minimum lenth={0}", sizeof(float)));
return ToSingle(buffer, 0);
}
/// <summary>
/// Change little-endian or big-endian to single
/// </summary>
/// <param name="buffer"></param>
/// <param name="bol"></param>
/// <returns></returns>
public static float ToSingle(byte[] buffer,BigOrLittle bol)
{
if (buffer.Length < sizeof(float))
throw new FormatException(string.Format("buffer does not contain enough bytes. Minimum lenth={0}", sizeof(float)));
if (bol == BigOrLittle.BigEndian) ReverseBytes(buffer);
return ToSingle( buffer, 0);
}
/// <summary>
/// Default change little-endian to single
/// </summary>
/// <param name="buffer"></param>
/// <param name="index">startindex</param>
/// <returns></returns>
public static float ToSingle(byte[] buffer,int index)
{
if (buffer.Length < sizeof(float))
throw new FormatException(string.Format("buffer does not contain enough bytes. Minimum lenth={0}", sizeof(float)));
return BitConverter.ToSingle(buffer, index);
}
#endregion
#region Get Double,Doubles from buffer
/// <summary>
/// Default change little-endian to single
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static double ToDouble(byte[] buffer)
{
if (buffer.Length < sizeof(double))
throw new FormatException(string.Format("buffer does not contain enough bytes. Minimum lenth={0}", sizeof(double)));
return ToDouble(buffer, 0);
}
/// <summary>
/// Change little-endian or big-endian to single
/// </summary>
/// <param name="buffer"></param>
/// <param name="bol"></param>
/// <returns></returns>
public static double ToDouble(byte[] buffer, BigOrLittle bol)
{
if (buffer.Length < sizeof(double))
throw new FormatException(string.Format("buffer does not contain enough bytes. Minimum lenth={0}", sizeof(double)));
if (bol == BigOrLittle.BigEndian) ReverseBytes(buffer);
return ToDouble(buffer, 0);
}
/// <summary>
/// Default change little-endian to single
/// </summary>
/// <param name="buffer"></param>
/// <param name="index">startindex</param>
/// <returns></returns>
public static double ToDouble(byte[] buffer, int index)
{
if (buffer.Length < sizeof(double))
throw new FormatException(string.Format("buffer does not contain enough bytes. Minimum lenth={0}", sizeof(double)));
return BitConverter.ToDouble(buffer, index);
}
#endregion
#region Reverse Bytes
/// <summary>
/// Reverse bytes array
/// </summary>
/// <param name="bytes"></param>
public static void ReverseBytes(byte[] bytes)
{
byte tmp;
int len = bytes.Length;
for (int i = 0; i < len / 2; i++)
{
tmp = bytes[len - 1 - i];
bytes[len - 1 - i] = bytes[i];
bytes[i] = tmp;
}
}
/// <summary>
/// Reverse bytes array
/// </summary>
/// <param name="bytes">buffer</param>
/// <param name="start">startindex</param>
/// <param name="len">lenth</param>
public static void ReverseBytes(byte[] bytes, int start, int len)
{
int end = start + len - 1;
byte tmp;
int i = 0;
for (int index = start; index < start + len / 2; index++, i++)
{
tmp = bytes[end - i];
bytes[end - i] = bytes[index];
bytes[index] = tmp;
}
}
/// <summary>
/// Reverse UInt16
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static UInt16 ReverseBytes(UInt16 value)
{
return (UInt16)((value & 0xFFU) << 8 | (value & 0xFF00U) >> 8);
}
/// <summary>
/// Reverse UInt32
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static UInt32 ReverseBytes(UInt32 value)
{
return (value & 0x000000FFU) << 24 | (value & 0x0000FF00U) << 8 |
(value & 0x00FF0000U) >> 8 | (value & 0xFF000000U) >> 24;
}
/// <summary>
/// Reverse UInt64
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static UInt64 ReverseBytes(UInt64 value)
{
return (value & 0x00000000000000FFUL) << 56 | (value & 0x000000000000FF00UL) << 40 |
(value & 0x0000000000FF0000UL) << 24 | (value & 0x00000000FF000000UL) << 8 |
(value & 0x000000FF00000000UL) >> 8 | (value & 0x0000FF0000000000UL) >> 24 |
(value & 0x00FF000000000000UL) >> 40 | (value & 0xFF00000000000000UL) >> 56;
}
#endregion
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/electronic-diy-studio/simple-scom.git
git@gitee.com:electronic-diy-studio/simple-scom.git
electronic-diy-studio
simple-scom
simpleSCOM
master

搜索帮助