CommTool.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Ant.Common
  8. {
  9. public class SocketCommTool
  10. {
  11. public static int ConvertDateTimeInt(System.DateTime time)
  12. {
  13. System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
  14. return (int)(time - startTime).TotalSeconds;
  15. }
  16. /// <summary>
  17. /// // 把字节型转换成十六进制字符串
  18. /// </summary>
  19. /// <param name="InBytes"></param>
  20. /// <returns></returns>
  21. public static string ByteToString(byte[] InBytes)
  22. {
  23. string StringOut = "";
  24. foreach (byte InByte in InBytes)
  25. {
  26. StringOut = StringOut + String.Format("{0:X2} ", InByte);
  27. }
  28. return StringOut;
  29. }
  30. public string ByteToString(byte[] InBytes, int len)
  31. {
  32. string StringOut = "";
  33. for (int i = 0; i < len; i++)
  34. {
  35. string str16 = String.Format("{0:X2} ", InBytes[i]);
  36. StringOut = StringOut + str16;
  37. }
  38. return StringOut;
  39. }
  40. // 把十六进制字符串转换成字节型
  41. public static byte[] StringToByte(string InString)
  42. {
  43. string[] ByteStrings;
  44. ByteStrings = InString.Split(" ".ToCharArray());
  45. byte[] ByteOut;
  46. ByteOut = new byte[ByteStrings.Length - 1];
  47. for (int i = 0; i == ByteStrings.Length - 1; i++)
  48. {
  49. ByteOut[i] = Convert.ToByte(("0x" + ByteStrings[i]));
  50. }
  51. return ByteOut;
  52. }
  53. public string ByteToASCII(byte[] InBytes)
  54. {
  55. string StringOut = "";
  56. foreach (byte InByte in InBytes)
  57. {
  58. string str = BitConverter.ToString(InBytes, 0, 2);
  59. //int iValue = Convert.ToInt32("0C", 16); // 16进制->10进制
  60. //int value = Convert.ToInt32(InByte.ToString(),16);
  61. byte[] bs = System.BitConverter.GetBytes(InByte); //int->byte[]
  62. string sValue = System.Text.Encoding.ASCII.GetString(bs).Substring(0, 1);
  63. string hexOutput = String.Format("{0:X}", InByte);
  64. StringOut = StringOut + sValue;
  65. }
  66. return StringOut;
  67. }
  68. /// <summary>
  69. /// 字节数组转16进制字符串
  70. /// </summary>
  71. /// <param name="bytes"></param>
  72. /// <returns></returns>
  73. public static string byteToHexStr(byte[] bytes)
  74. {
  75. string returnStr = "";
  76. if (bytes != null)
  77. {
  78. for (int i = 0; i < bytes.Length; i++)
  79. {
  80. returnStr += bytes[i].ToString("X2");
  81. }
  82. }
  83. return returnStr;
  84. }
  85. public string StrToHex(string mStr) //返回处理后的十六进制字符串
  86. {
  87. return BitConverter.ToString(
  88. ASCIIEncoding.Default.GetBytes(mStr)).Replace("-", " ");
  89. } /* StrToHex */
  90. public string HexToStr(string mHex) // 返回十六进制代表的字符串
  91. {
  92. mHex = mHex.Replace(" ", "");
  93. if (mHex.Length <= 0) return "";
  94. byte[] vBytes = new byte[mHex.Length / 2];
  95. for (int i = 0; i < mHex.Length; i += 2)
  96. if (!byte.TryParse(mHex.Substring(i, 2), NumberStyles.HexNumber, null, out vBytes[i / 2]))
  97. vBytes[i / 2] = 0;
  98. return ASCIIEncoding.Default.GetString(vBytes);
  99. } /* HexToStr */
  100. /// <summary>
  101. /// 十六进制转成数组
  102. /// </summary>
  103. /// <param name="mHex"></param>
  104. /// <returns></returns>
  105. public static byte[] HexTobyte(string mHex) // 返回十六进制代表的字符串
  106. {
  107. mHex = mHex.Replace(" ", "");
  108. byte[] vBytes = new byte[mHex.Length / 2];
  109. for (int i = 0; i < mHex.Length; i += 2)
  110. if (!byte.TryParse(mHex.Substring(i, 2), NumberStyles.HexNumber, null, out vBytes[i / 2]))
  111. vBytes[i / 2] = 0;
  112. return vBytes;
  113. }
  114. /// <summary>
  115. /// 16进制字符串转字节数组
  116. /// </summary>
  117. /// <param name="hexString"></param>
  118. /// <returns></returns>
  119. private static byte[] strToToHexByte(string hexString)
  120. {
  121. hexString = hexString.Replace(" ", "");
  122. if ((hexString.Length % 2) != 0)
  123. hexString += " ";
  124. byte[] returnBytes = new byte[hexString.Length / 2];
  125. for (int i = 0; i < returnBytes.Length; i++)
  126. returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Trim(), 16);
  127. return returnBytes;
  128. }
  129. /// <summary>
  130. /// 从汉字转换到16进制
  131. // </summary>
  132. /// <param name="s"></param>
  133. /// <param name="charset">编码,如"utf-8","gb2312"</param>
  134. /// <param name="fenge">是否每字符用逗号分隔</param>
  135. /// <returns></returns>
  136. public static string ToHex(string s, string charset, bool fenge)
  137. {
  138. if ((s.Length % 2) != 0)
  139. {
  140. s += " ";//空格
  141. //throw new ArgumentException("s is not valid chinese string!");
  142. }
  143. System.Text.Encoding chs = System.Text.Encoding.GetEncoding(charset);
  144. byte[] bytes = chs.GetBytes(s);
  145. string str = "";
  146. for (int i = 0; i < bytes.Length; i++)
  147. {
  148. str += string.Format("{0:X}", bytes[i]);
  149. if (fenge && (i != bytes.Length - 1))
  150. {
  151. str += string.Format("{0}", ",");
  152. }
  153. }
  154. return str.ToLower();
  155. }
  156. /// <summary>
  157. /// 给车发送开锁还是锁车
  158. /// </summary>
  159. /// <param name="parkname">包名</param>
  160. /// <param name="state">状态</param>
  161. /// <returns></returns>
  162. public static string sendHalderHex(string parkname, int state)
  163. {
  164. var str = ""; string headstr = "FAAF"; string footstr = "FEEF";
  165. char[] values = parkname.ToCharArray();
  166. foreach (char letter in values)
  167. {
  168. int value = Convert.ToInt32(letter);
  169. str += String.Format("{0:X2}", value);
  170. }
  171. str += "000200";
  172. str += String.Format("{0:X2}", state);
  173. str = headstr + str + "00" + footstr;
  174. return str;
  175. }
  176. /// <summary>
  177. ///
  178. /// </summary>
  179. /// <param name="time"></param>
  180. /// <returns></returns>
  181. public static string sendCarInfo(int time)
  182. {
  183. var str = ""; string headstr = "FAAF"; string footstr = "FEEF";
  184. string parkname = "SB02";
  185. char[] values = parkname.ToCharArray();
  186. foreach (char letter in values)
  187. {
  188. int value = Convert.ToInt32(letter);
  189. str += String.Format("{0:X2}", value);
  190. }
  191. str += "000400";
  192. string s = Convert.ToString(time, 16).ToUpper();
  193. string s1 = s.Substring(0, 2);
  194. string s2 = s.Substring(2, 2);
  195. string s3 = s.Substring(4, 2);
  196. string s4 = s.Substring(6, 2);
  197. string ss = s4 + s3 + s2 + s1;
  198. //string s = time.ToString();
  199. str += ss;
  200. str = headstr + str + footstr;
  201. return str;
  202. }
  203. /// <summary>
  204. /// 从16进制转换成汉字
  205. /// </summary>
  206. /// <param name="hex"></param>
  207. /// <param name="charset">编码,如"utf-8","gb2312"</param>
  208. /// <returns></returns>
  209. public static string UnHex(string hex, string charset)
  210. {
  211. if (hex == null)
  212. throw new ArgumentNullException("hex");
  213. hex = hex.Replace(",", "");
  214. hex = hex.Replace("/n", "");
  215. hex = hex.Replace("//", "");
  216. hex = hex.Replace(" ", "");
  217. if (hex.Length % 2 != 0)
  218. {
  219. hex += "20";//空格
  220. }
  221. // 需要将 hex 转换成 byte 数组。
  222. byte[] bytes = new byte[hex.Length / 2];
  223. for (int i = 0; i < bytes.Length; i++)
  224. {
  225. try
  226. {
  227. // 每两个字符是一个 byte。
  228. bytes[i] = byte.Parse(hex.Substring(i * 2, 2),
  229. System.Globalization.NumberStyles.HexNumber);
  230. }
  231. catch
  232. {
  233. // Rethrow an exception with custom message.
  234. throw new ArgumentException("hex is not a valid hex number!", "hex");
  235. }
  236. }
  237. System.Text.Encoding chs = System.Text.Encoding.GetEncoding(charset);
  238. return chs.GetString(bytes);
  239. }
  240. }
  241. }