/DataServiceCenter/DataServiceCenter/CodeConverter.cs
C# | 427 lines | 317 code | 19 blank | 91 comment | 51 complexity | 36f7296ca4d41dd53a123940b92dbd3b MD5 | raw file
- using System;
- namespace DataServiceCenter
- {
- /// <summary>
- /// 类型转换类
- /// </summary>
- public class CodeConverter
- {
- public CodeConverter()
- {
- //
- // TODO: 在此处添加构造函数逻辑
- //
- }
- /// <summary>
- /// 返回一个byte[]从第index个元素后长度为length的byte[]
- /// </summary>
- /// <param name="by"></param>
- /// <param name="index"></param>
- /// <param name="length"></param>
- /// <returns></returns>
- public static byte[] SubByteArray(byte[] by, int index, int length)
- {
- byte[] byr = new byte[length];
- for (int i = index; i < index + length; i++)
- {
- byr[i - index] = by[i];
- }
- return byr;
- }
- /// <summary>
- /// 返回一个byte[]从第index个元素后的byte[]
- /// </summary>
- /// <param name="by"></param>
- /// <param name="index"></param>
- /// <param name="length"></param>
- /// <returns></returns>
- public static byte[] SubByteArray(byte[] by, int index)
- {
- return SubByteArray(by, index, by.Length - index);
-
- }
- /// <summary>
- /// 转换数据为Dtu数据格式
- /// </summary>
- /// <param name="by">要转换的字节数组</param>
- /// <returns>转换后的字节数组</returns>
- public static byte[] ConverBytesToDtuFormat(byte[] by)
- {
- int i;
- int length = by.Length;
- byte[] buffer1 = new byte[length * 3];
- byte[] buffer2 = new byte[length * 3];
- int counter1 = 0;
- int counter2 = 0;
- byte bytefe = 0xfe;
- byte bytefd = 0xfd;
- byte byteed = 0xed;
- byte byteee = 0xee;
-
- for (i = 0; i < length; i++)
- {
- if (by[i] != bytefd)
- {
- buffer1[counter1++] = by[i];
- }
- else//数据中fd 转换为 fd ed
- {
- buffer1[counter1++] = bytefd;
- buffer1[counter1++] = byteed;
- }
- }
- for (i = 0; i < counter1; i++)
- {
- if (buffer1[i] != bytefe)
- {
- buffer2[counter2++] = buffer1[i];
- }
- else//数据中fe 转换为 fd ee
- {
- buffer2[counter2++] = bytefd;
- buffer2[counter2++] = byteee;
- }
- }
- byte[] buffer3 = new byte[counter2];//转换结果数据
- for (i = 0; i < counter2; i++)
- {
- buffer3[i] = buffer2[i];
- }
- return buffer3;
- }
- /// <summary>
- /// 获得大数据包数据
- /// </summary>
- /// <param name="data">原始字节数组</param>
- /// <returns>转换后的字节数组</returns>
- public static byte[] GetBigData(byte[] data)//大数据包模式下前5字节去掉
- {
- int num = data.Length - 5;
- byte[] buffer = new byte[num];
- for (int i = 0; i < num; i++)
- {
- buffer[i] = data[i + 4];
- }
- return buffer;
- }
- public static byte[] GetBigData(DtuArgs dtuArgs)//大数据包模式下前5字节去掉
- {
- int num = dtuArgs.BytesReceived - 5;
- byte[] buffer = new byte[num];
- for (int i = 0; i < num; i++)
- {
- buffer[i] = dtuArgs.ReceiveBuffer[dtuArgs.Offset+i + 4];
- }
- return buffer;
- }
- /// <summary>
- /// 获得注册数据包中的DtuID
- /// </summary>
- /// <param name="by">原始注册数据包数据</param>
- /// <returns>DtuID</returns>
- public static string GetDtuID(byte[] by)
- {
- string str = "";
- for (int i = 3; i >= 0; i--)
- {
- str = str + Convert.ToString(by[i], 0x10).PadLeft(2, '0');
- }
- return str;
- }
-
- public static string GetDtuID(DtuArgs dtuArgs)
- {
- string str = "";
- for (int i = 3; i >= 0; i--)
- {
- str = str + Convert.ToString(dtuArgs.ReceiveBuffer[dtuArgs.Offset+i], 0x10).PadLeft(2, '0');
- }
- return str;
- }
-
- /// <summary>
- /// 转换数据中的每个字节
- /// </summary>
- /// <param name="data">要转换的字节数组</param>
- /// <returns>转换后的字节数组</returns>
- public static byte[] DecodeBytes(byte[] data)
- {
- int i;
- int length = data.Length;
- byte[] buffer1 = new byte[length + 1];
- byte[] buffer2 = new byte[length + 1];
- int counter1 = 0;
- int counter2 = 0;
- int counter3 = 0;
- byte bytefe = 0xfe;
- byte bytefd = 0xfd;
- byte byteed = 0xed;
- byte byteee = 0xee;
-
- for (i = 0; i < length; i++)
- {
- if (data[i] != bytefe)
- {
- buffer1[counter1++] = data[i];//收集有效数据
- }
- }
- for (i = 0; i < counter1; i++)
- {
- if ((buffer1[i] == bytefd) && (buffer1[i + 1] == byteed))
- {
- buffer2[counter2++] = bytefd;//数据中fd ed 转换为 fd
- i++;
- }
- else
- {
- buffer2[counter2++] = buffer1[i];//其他不变
- }
- }
- for (i = 0; i < counter2; i++)
- {
- if ((buffer2[i] == bytefd) && (buffer2[i + 1] == byteee))
- {
- buffer1[counter3++] = bytefe;//数据中fd ee 转换为 fe
- i++;
- }
- else
- {
- buffer1[counter3++] = buffer2[i];//其他不变
- }
- }
- byte[] resultbuffer = new byte[counter3];
- for (i = 0; i < counter3; i++)
- {
- resultbuffer[i] = buffer1[i];//转换后结果数据
- }
- return resultbuffer;
- }
- public static byte[] DecodeBytes(DtuArgs dtuArgs)
- {
- int i;
- int length = dtuArgs.BytesReceived;
- byte[] buffer1 = new byte[length + 1];
- byte[] buffer2 = new byte[length + 1];
- int counter1 = 0;
- int counter2 = 0;
- int counter3 = 0;
- byte bytefe = 0xfe;
- byte bytefd = 0xfd;
- byte byteed = 0xed;
- byte byteee = 0xee;
-
- for (i = 0; i < length; i++)
- {
- if (dtuArgs.ReceiveBuffer[dtuArgs.Offset+ i] != bytefe)
- {
- buffer1[counter1++] = dtuArgs.ReceiveBuffer[dtuArgs.Offset+ i];//收集有效数据
- }
- }
- for (i = 0; i < counter1; i++)
- {
- if ((buffer1[i] == bytefd) && (buffer1[i + 1] == byteed))
- {
- buffer2[counter2++] = bytefd;//数据中fd ed 转换为 fd
- i++;
- }
- else
- {
- buffer2[counter2++] = buffer1[i];//其他不变
- }
- }
- for (i = 0; i < counter2; i++)
- {
- if ((buffer2[i] == bytefd) && (buffer2[i + 1] == byteee))
- {
- buffer1[counter3++] = bytefe;//数据中fd ee 转换为 fe
- i++;
- }
- else
- {
- buffer1[counter3++] = buffer2[i];//其他不变
- }
- }
- byte[] resultbuffer = new byte[counter3];
- for (i = 0; i < counter3; i++)
- {
- resultbuffer[i] = buffer1[i];//转换后结果数据
- }
- return resultbuffer;
- }
-
-
- /// <summary>
- /// 返回两个byte数组连接后的byte数组。.
- /// </summary>
- public static byte[] ByteAdd(byte[] by1, byte[] by2)
- {
- int l1 = by1.Length;
- int l2 = by2.Length;
- byte[] by = new byte[l1 + l2];
- for (int i = 0; i < l1; i++)
- {
- by[i] = by1[i];
- }
- for (int i = 0; i < l2; i++)
- {
- by[i + l1] = by2[i];
- }
- return by;
- }
- /// <summary>
- /// "aabbcc"变为"ccbbaa"
- /// </summary>
- /// <param name="str">长度必需是偶数</param>
- /// <returns></returns>
- public static string StrTOStrV(string str)
- {
- string retn = "";
- int length = str.Length;
- for (int i = 0; i <= length - 2; i += 2)
- {
- retn = str.Substring(i, 2) + retn;
- }
- return retn;
- }
-
-
- /// <summary>
- /// 把一个字节数组转化成十六进制的字符串形式,以空格隔开。
- /// </summary>
- public static string ByteToHexStr(byte[] da)
- {
- string s = "";
- for (int i = 0; i < da.Length; i++)
- {
- s += Convert.ToString(da[i], 16).PadLeft(2, '0') + " ";
- }
- return s;
- }
-
- public static string ByteToHexStr(DtuArgs dtuArgs)
- {
- string s = "";
- for (int i = 0; i < dtuArgs.BytesReceived; i++)
- {
- s += Convert.ToString(dtuArgs.ReceiveBuffer[dtuArgs.Offset+i], 16).PadLeft(2, '0') + " ";
- }
- return s;
- }
- /// <summary>
- /// 把一个字节数组转化成十六进制的字符串形式
- /// </summary>
- public static string ByteToHexStr2(byte[] da)
- {
- string s = "";
- for (int i = 0; i < da.Length; i++)
- {
- s += Convert.ToString(da[i], 16).PadLeft(2, '0');
- }
- return s;
- }
- /// <summary>
- /// 把诸如:23 fe e3 的字符串转化成byte[]
- /// </summary>
- /// <param name="da"></param>
- /// <returns></returns>
- public static byte[] StrToHexByte(string da)
- {
- string sends = da;
- sends = sends.Replace(" ", "");//去掉空格
- sends = sends.Replace("\n", "");
- sends = sends.Replace("\r", "");
- int length = sends.Length / 2;
- byte[] sendb = new byte[length];
-
- for (int i = 0; i < length; i++)
- {
- sendb[i] = Convert.ToByte(sends.Substring(i * 2, 2), 16);
- }
-
- return (sendb);
- }
- /// <summary>
- /// 判断是否数字
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static bool IsDigit(string str)//判断是否数字
- {
- for (int i = 0; i < str.Length; i++)
- {
- if (!(str[i] >= '0' && str[i] <= '9'))
- return false;
- }
- return true;
- }
- /// <summary>
- /// 把8位字符byt的从第begin字符到end字符转化为二进制字符串
- /// </summary>
- /// <param name="begin"></param>
- /// <param name="end"></param>
- /// <param name="byt"></param>
- /// <returns></returns>
- public static string BitDivision(int begin, int end, byte byt)
- {
- string str1 = "1";
- str1 = str1.PadRight(end - begin + 1, '1');
- str1 = str1.PadLeft(end, '0');
- str1 = str1.PadRight(8, '0');
- int a = Convert.ToInt32(str1, 2);
- int b = byt;
- int c = a & b;
- c = c >> (8 - end);
- return Convert.ToString(c, 2).PadLeft(end - begin + 1, '0');
- }
- /// <summary>
- /// 把二进制字符串转化为十六进制字符串.
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static string ToSix(string str)
- {
- long l = Convert.ToInt64(str, 2);
- return "0x" + Convert.ToString(l, 16).PadLeft(2, '0');
- }
- /// <summary>
- /// 把二进制字符串转化为十进制字符串
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static string ToTen(string str)
- {
- long l = Convert.ToInt64(str, 2);
- return Convert.ToString(l, 10);
- }
- /// <summary>
- /// "ef"-----"11101111"
- /// </summary>
- /// <param name="hex"></param>
- /// <returns></returns>
- public static string HexStrToBinStr(string hex)
- {
- int l = hex.Length * 4;
- return Convert.ToString(Convert.ToInt32(hex, 16), 2).PadLeft(l, '0');
-
- }
- /// <summary>
- /// "1110010"--------"1,1,1,0,0,1,0"
- /// </summary>
- /// <param name="bin"></param>
- /// <returns></returns>
- public static string BinDotBin(string bin)
- {
- string rtn = "";
- for (int i = 0; i < bin.Length; i++)
- {
- rtn += bin.Substring(i, 1) + ",";
-
- }
- return rtn.TrimEnd(new char[] { ',' });
- }
-
-
-
- }
- }
-