PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/DataServiceCenter/DataServiceCenter/CodeConverter.cs

#
C# | 427 lines | 317 code | 19 blank | 91 comment | 51 complexity | 36f7296ca4d41dd53a123940b92dbd3b MD5 | raw file
  1. using System;
  2. namespace DataServiceCenter
  3. {
  4. /// <summary>
  5. /// 类型转换类
  6. /// </summary>
  7. public class CodeConverter
  8. {
  9. public CodeConverter()
  10. {
  11. //
  12. // TODO: 在此处添加构造函数逻辑
  13. //
  14. }
  15. /// <summary>
  16. /// 返回一个byte[]从第index个元素后长度为length的byte[]
  17. /// </summary>
  18. /// <param name="by"></param>
  19. /// <param name="index"></param>
  20. /// <param name="length"></param>
  21. /// <returns></returns>
  22. public static byte[] SubByteArray(byte[] by, int index, int length)
  23. {
  24. byte[] byr = new byte[length];
  25. for (int i = index; i < index + length; i++)
  26. {
  27. byr[i - index] = by[i];
  28. }
  29. return byr;
  30. }
  31. /// <summary>
  32. /// 返回一个byte[]从第index个元素后的byte[]
  33. /// </summary>
  34. /// <param name="by"></param>
  35. /// <param name="index"></param>
  36. /// <param name="length"></param>
  37. /// <returns></returns>
  38. public static byte[] SubByteArray(byte[] by, int index)
  39. {
  40. return SubByteArray(by, index, by.Length - index);
  41. }
  42. /// <summary>
  43. /// 转换数据为Dtu数据格式
  44. /// </summary>
  45. /// <param name="by">要转换的字节数组</param>
  46. /// <returns>转换后的字节数组</returns>
  47. public static byte[] ConverBytesToDtuFormat(byte[] by)
  48. {
  49. int i;
  50. int length = by.Length;
  51. byte[] buffer1 = new byte[length * 3];
  52. byte[] buffer2 = new byte[length * 3];
  53. int counter1 = 0;
  54. int counter2 = 0;
  55. byte bytefe = 0xfe;
  56. byte bytefd = 0xfd;
  57. byte byteed = 0xed;
  58. byte byteee = 0xee;
  59. for (i = 0; i < length; i++)
  60. {
  61. if (by[i] != bytefd)
  62. {
  63. buffer1[counter1++] = by[i];
  64. }
  65. else//数据中fd 转换为 fd ed
  66. {
  67. buffer1[counter1++] = bytefd;
  68. buffer1[counter1++] = byteed;
  69. }
  70. }
  71. for (i = 0; i < counter1; i++)
  72. {
  73. if (buffer1[i] != bytefe)
  74. {
  75. buffer2[counter2++] = buffer1[i];
  76. }
  77. else//数据中fe 转换为 fd ee
  78. {
  79. buffer2[counter2++] = bytefd;
  80. buffer2[counter2++] = byteee;
  81. }
  82. }
  83. byte[] buffer3 = new byte[counter2];//转换结果数据
  84. for (i = 0; i < counter2; i++)
  85. {
  86. buffer3[i] = buffer2[i];
  87. }
  88. return buffer3;
  89. }
  90. /// <summary>
  91. /// 获得大数据包数据
  92. /// </summary>
  93. /// <param name="data">原始字节数组</param>
  94. /// <returns>转换后的字节数组</returns>
  95. public static byte[] GetBigData(byte[] data)//大数据包模式下前5字节去掉
  96. {
  97. int num = data.Length - 5;
  98. byte[] buffer = new byte[num];
  99. for (int i = 0; i < num; i++)
  100. {
  101. buffer[i] = data[i + 4];
  102. }
  103. return buffer;
  104. }
  105. public static byte[] GetBigData(DtuArgs dtuArgs)//大数据包模式下前5字节去掉
  106. {
  107. int num = dtuArgs.BytesReceived - 5;
  108. byte[] buffer = new byte[num];
  109. for (int i = 0; i < num; i++)
  110. {
  111. buffer[i] = dtuArgs.ReceiveBuffer[dtuArgs.Offset+i + 4];
  112. }
  113. return buffer;
  114. }
  115. /// <summary>
  116. /// 获得注册数据包中的DtuID
  117. /// </summary>
  118. /// <param name="by">原始注册数据包数据</param>
  119. /// <returns>DtuID</returns>
  120. public static string GetDtuID(byte[] by)
  121. {
  122. string str = "";
  123. for (int i = 3; i >= 0; i--)
  124. {
  125. str = str + Convert.ToString(by[i], 0x10).PadLeft(2, '0');
  126. }
  127. return str;
  128. }
  129. public static string GetDtuID(DtuArgs dtuArgs)
  130. {
  131. string str = "";
  132. for (int i = 3; i >= 0; i--)
  133. {
  134. str = str + Convert.ToString(dtuArgs.ReceiveBuffer[dtuArgs.Offset+i], 0x10).PadLeft(2, '0');
  135. }
  136. return str;
  137. }
  138. /// <summary>
  139. /// 转换数据中的每个字节
  140. /// </summary>
  141. /// <param name="data">要转换的字节数组</param>
  142. /// <returns>转换后的字节数组</returns>
  143. public static byte[] DecodeBytes(byte[] data)
  144. {
  145. int i;
  146. int length = data.Length;
  147. byte[] buffer1 = new byte[length + 1];
  148. byte[] buffer2 = new byte[length + 1];
  149. int counter1 = 0;
  150. int counter2 = 0;
  151. int counter3 = 0;
  152. byte bytefe = 0xfe;
  153. byte bytefd = 0xfd;
  154. byte byteed = 0xed;
  155. byte byteee = 0xee;
  156. for (i = 0; i < length; i++)
  157. {
  158. if (data[i] != bytefe)
  159. {
  160. buffer1[counter1++] = data[i];//收集有效数据
  161. }
  162. }
  163. for (i = 0; i < counter1; i++)
  164. {
  165. if ((buffer1[i] == bytefd) && (buffer1[i + 1] == byteed))
  166. {
  167. buffer2[counter2++] = bytefd;//数据中fd ed 转换为 fd
  168. i++;
  169. }
  170. else
  171. {
  172. buffer2[counter2++] = buffer1[i];//其他不变
  173. }
  174. }
  175. for (i = 0; i < counter2; i++)
  176. {
  177. if ((buffer2[i] == bytefd) && (buffer2[i + 1] == byteee))
  178. {
  179. buffer1[counter3++] = bytefe;//数据中fd ee 转换为 fe
  180. i++;
  181. }
  182. else
  183. {
  184. buffer1[counter3++] = buffer2[i];//其他不变
  185. }
  186. }
  187. byte[] resultbuffer = new byte[counter3];
  188. for (i = 0; i < counter3; i++)
  189. {
  190. resultbuffer[i] = buffer1[i];//转换后结果数据
  191. }
  192. return resultbuffer;
  193. }
  194. public static byte[] DecodeBytes(DtuArgs dtuArgs)
  195. {
  196. int i;
  197. int length = dtuArgs.BytesReceived;
  198. byte[] buffer1 = new byte[length + 1];
  199. byte[] buffer2 = new byte[length + 1];
  200. int counter1 = 0;
  201. int counter2 = 0;
  202. int counter3 = 0;
  203. byte bytefe = 0xfe;
  204. byte bytefd = 0xfd;
  205. byte byteed = 0xed;
  206. byte byteee = 0xee;
  207. for (i = 0; i < length; i++)
  208. {
  209. if (dtuArgs.ReceiveBuffer[dtuArgs.Offset+ i] != bytefe)
  210. {
  211. buffer1[counter1++] = dtuArgs.ReceiveBuffer[dtuArgs.Offset+ i];//收集有效数据
  212. }
  213. }
  214. for (i = 0; i < counter1; i++)
  215. {
  216. if ((buffer1[i] == bytefd) && (buffer1[i + 1] == byteed))
  217. {
  218. buffer2[counter2++] = bytefd;//数据中fd ed 转换为 fd
  219. i++;
  220. }
  221. else
  222. {
  223. buffer2[counter2++] = buffer1[i];//其他不变
  224. }
  225. }
  226. for (i = 0; i < counter2; i++)
  227. {
  228. if ((buffer2[i] == bytefd) && (buffer2[i + 1] == byteee))
  229. {
  230. buffer1[counter3++] = bytefe;//数据中fd ee 转换为 fe
  231. i++;
  232. }
  233. else
  234. {
  235. buffer1[counter3++] = buffer2[i];//其他不变
  236. }
  237. }
  238. byte[] resultbuffer = new byte[counter3];
  239. for (i = 0; i < counter3; i++)
  240. {
  241. resultbuffer[i] = buffer1[i];//转换后结果数据
  242. }
  243. return resultbuffer;
  244. }
  245. /// <summary>
  246. /// 返回两个byte数组连接后的byte数组。.
  247. /// </summary>
  248. public static byte[] ByteAdd(byte[] by1, byte[] by2)
  249. {
  250. int l1 = by1.Length;
  251. int l2 = by2.Length;
  252. byte[] by = new byte[l1 + l2];
  253. for (int i = 0; i < l1; i++)
  254. {
  255. by[i] = by1[i];
  256. }
  257. for (int i = 0; i < l2; i++)
  258. {
  259. by[i + l1] = by2[i];
  260. }
  261. return by;
  262. }
  263. /// <summary>
  264. /// "aabbcc"变为"ccbbaa"
  265. /// </summary>
  266. /// <param name="str">长度必需是偶数</param>
  267. /// <returns></returns>
  268. public static string StrTOStrV(string str)
  269. {
  270. string retn = "";
  271. int length = str.Length;
  272. for (int i = 0; i <= length - 2; i += 2)
  273. {
  274. retn = str.Substring(i, 2) + retn;
  275. }
  276. return retn;
  277. }
  278. /// <summary>
  279. /// 把一个字节数组转化成十六进制的字符串形式,以空格隔开。
  280. /// </summary>
  281. public static string ByteToHexStr(byte[] da)
  282. {
  283. string s = "";
  284. for (int i = 0; i < da.Length; i++)
  285. {
  286. s += Convert.ToString(da[i], 16).PadLeft(2, '0') + " ";
  287. }
  288. return s;
  289. }
  290. public static string ByteToHexStr(DtuArgs dtuArgs)
  291. {
  292. string s = "";
  293. for (int i = 0; i < dtuArgs.BytesReceived; i++)
  294. {
  295. s += Convert.ToString(dtuArgs.ReceiveBuffer[dtuArgs.Offset+i], 16).PadLeft(2, '0') + " ";
  296. }
  297. return s;
  298. }
  299. /// <summary>
  300. /// 把一个字节数组转化成十六进制的字符串形式
  301. /// </summary>
  302. public static string ByteToHexStr2(byte[] da)
  303. {
  304. string s = "";
  305. for (int i = 0; i < da.Length; i++)
  306. {
  307. s += Convert.ToString(da[i], 16).PadLeft(2, '0');
  308. }
  309. return s;
  310. }
  311. /// <summary>
  312. /// 把诸如:23 fe e3 的字符串转化成byte[]
  313. /// </summary>
  314. /// <param name="da"></param>
  315. /// <returns></returns>
  316. public static byte[] StrToHexByte(string da)
  317. {
  318. string sends = da;
  319. sends = sends.Replace(" ", "");//去掉空格
  320. sends = sends.Replace("\n", "");
  321. sends = sends.Replace("\r", "");
  322. int length = sends.Length / 2;
  323. byte[] sendb = new byte[length];
  324. for (int i = 0; i < length; i++)
  325. {
  326. sendb[i] = Convert.ToByte(sends.Substring(i * 2, 2), 16);
  327. }
  328. return (sendb);
  329. }
  330. /// <summary>
  331. /// 判断是否数字
  332. /// </summary>
  333. /// <param name="str"></param>
  334. /// <returns></returns>
  335. public static bool IsDigit(string str)//判断是否数字
  336. {
  337. for (int i = 0; i < str.Length; i++)
  338. {
  339. if (!(str[i] >= '0' && str[i] <= '9'))
  340. return false;
  341. }
  342. return true;
  343. }
  344. /// <summary>
  345. /// 把8位字符byt的从第begin字符到end字符转化为二进制字符串
  346. /// </summary>
  347. /// <param name="begin"></param>
  348. /// <param name="end"></param>
  349. /// <param name="byt"></param>
  350. /// <returns></returns>
  351. public static string BitDivision(int begin, int end, byte byt)
  352. {
  353. string str1 = "1";
  354. str1 = str1.PadRight(end - begin + 1, '1');
  355. str1 = str1.PadLeft(end, '0');
  356. str1 = str1.PadRight(8, '0');
  357. int a = Convert.ToInt32(str1, 2);
  358. int b = byt;
  359. int c = a & b;
  360. c = c >> (8 - end);
  361. return Convert.ToString(c, 2).PadLeft(end - begin + 1, '0');
  362. }
  363. /// <summary>
  364. /// 把二进制字符串转化为十六进制字符串.
  365. /// </summary>
  366. /// <param name="str"></param>
  367. /// <returns></returns>
  368. public static string ToSix(string str)
  369. {
  370. long l = Convert.ToInt64(str, 2);
  371. return "0x" + Convert.ToString(l, 16).PadLeft(2, '0');
  372. }
  373. /// <summary>
  374. /// 把二进制字符串转化为十进制字符串
  375. /// </summary>
  376. /// <param name="str"></param>
  377. /// <returns></returns>
  378. public static string ToTen(string str)
  379. {
  380. long l = Convert.ToInt64(str, 2);
  381. return Convert.ToString(l, 10);
  382. }
  383. /// <summary>
  384. /// "ef"-----"11101111"
  385. /// </summary>
  386. /// <param name="hex"></param>
  387. /// <returns></returns>
  388. public static string HexStrToBinStr(string hex)
  389. {
  390. int l = hex.Length * 4;
  391. return Convert.ToString(Convert.ToInt32(hex, 16), 2).PadLeft(l, '0');
  392. }
  393. /// <summary>
  394. /// "1110010"--------"1,1,1,0,0,1,0"
  395. /// </summary>
  396. /// <param name="bin"></param>
  397. /// <returns></returns>
  398. public static string BinDotBin(string bin)
  399. {
  400. string rtn = "";
  401. for (int i = 0; i < bin.Length; i++)
  402. {
  403. rtn += bin.Substring(i, 1) + ",";
  404. }
  405. return rtn.TrimEnd(new char[] { ',' });
  406. }
  407. }
  408. }