DataFormatConversion.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace BaseLibRWFile
  7. {
  8. public class DataFormatConversion
  9. {
  10. #region ByteToString
  11. /// <summary>
  12. /// 字节数组到十六进制字符串
  13. /// </summary>
  14. /// <param name="bytes">待转换字节数组</param>
  15. /// <returns>十六进制字符串</returns>
  16. public static string ByteToHexStr(byte[] bytes)//byte数组转换为十六进制字符串
  17. {
  18. string returnStr = "";
  19. if (bytes != null)
  20. {
  21. for (int i = 0; i < bytes.Length; i++)
  22. {
  23. returnStr += bytes[i].ToString("X2");//ToString("X2") 为C#中的字符串格式控制符
  24. }
  25. }
  26. return returnStr;
  27. }
  28. /// <summary>
  29. /// Bytes to string.
  30. /// </summary>
  31. /// <param name="data">The data.</param>
  32. /// <returns>System.String.</returns>
  33. public static string ByteToString(byte[] data)
  34. {
  35. return Encoding.Default.GetString(data);
  36. }
  37. //字节变量返回8为二进制字符串
  38. /// <summary>
  39. /// 字节变量返回8位二进制字符串
  40. /// </summary>
  41. /// <param name="bValue">字节变量值</param>
  42. /// <returns></returns>
  43. public string ByteToBinary(byte bValue)
  44. {
  45. string strTemp;
  46. int i, strLen;
  47. strTemp = Convert.ToString(bValue, 2);
  48. if (strTemp.Length < 8)
  49. {
  50. strLen = strTemp.Length;
  51. for (i = 0; i < 8 - strLen; i++)
  52. {
  53. strTemp = "0" + strTemp;
  54. }
  55. }
  56. return strTemp;
  57. }
  58. #endregion
  59. /// <summary>
  60. /// 字符串转换为16进制
  61. /// </summary>
  62. /// <param name="str"></param>
  63. /// <returns></returns>
  64. public static string StringToHexString(string str)
  65. {
  66. StringBuilder s = new StringBuilder();
  67. foreach (short c in str.ToCharArray())
  68. {
  69. s.Append(c.ToString("X4"));
  70. }
  71. return s.ToString();
  72. }
  73. /// <summary>
  74. /// 字符串转16进制字节数组
  75. /// </summary>
  76. /// <param name="hexString">待转换字符串</param>
  77. /// <returns>字节数组</returns>
  78. public static byte[] HexStrTobyte(string hexString)//十六进制字符串转换为byte数组
  79. {
  80. hexString = hexString.Replace(" ", "");
  81. if ((hexString.Length % 2) != 0)
  82. hexString += " ";
  83. byte[] returnBytes = new byte[hexString.Length / 2];
  84. for (int i = 0; i < returnBytes.Length; i++)
  85. returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Trim(), 16);
  86. return returnBytes;
  87. }
  88. /// <summary>
  89. /// Strings to byte.
  90. /// </summary>
  91. /// <param name="value">The value.</param>
  92. /// <returns>System.Byte[].</returns>
  93. public static byte[] StringToByte(string value)
  94. {
  95. return Encoding.Default.GetBytes(value);
  96. }
  97. /// <summary>
  98. /// 字符串转换16进制字节数组
  99. /// </summary>
  100. /// <param name="hexString"></param>
  101. /// <returns></returns>
  102. public static byte[] StrToHexByte(string hexString)
  103. {
  104. hexString = hexString.Replace(" ", "");
  105. if ((hexString.Length % 2) != 0)
  106. hexString += " ";
  107. byte[] returnBytes = new byte[hexString.Length / 2];
  108. for (int i = 0; i < returnBytes.Length; i++)
  109. returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Replace(" ", ""), 16);
  110. return returnBytes;
  111. }
  112. public static string StrToHexBytes(string hexString)
  113. {
  114. hexString = hexString.Trim();
  115. byte[] b = Encoding.ASCII.GetBytes(hexString);//按照指定编码将string编程字节数组
  116. string[] strA = new string[b.Length];
  117. string strB = string.Empty;
  118. for (int i = 0; i < b.Length; i++)//逐字节变为16进制字符
  119. {
  120. strA[i] = Convert.ToString(b[i], 16);
  121. }
  122. int il = (int)Math.Ceiling((double)b.Length / 2);
  123. for (int i = 0; i < il; i++)
  124. {
  125. if (i * 2 + 1 > strA.Length - 1)
  126. {
  127. strB += "00";
  128. }
  129. else
  130. {
  131. strB += strA[i * 2 + 1];
  132. }
  133. strB += strA[i * 2];
  134. }
  135. return strB;
  136. }
  137. public static string HexStringToBinary(string[] str)
  138. {
  139. byte[] buff = new byte[str.Length * 2];
  140. for (int i = 0; i < str.Length; i++)
  141. {
  142. buff[i * 2] = Convert.ToByte(str[i].Substring(2, 2), 16);
  143. buff[i * 2 + 1] = Convert.ToByte(str[i].Substring(0, 2), 16);
  144. }
  145. string result = Encoding.Default.GetString(buff);
  146. result = result.Trim();
  147. result = result.TrimEnd('\0');
  148. result = result.TrimStart('\0');
  149. return result;
  150. }
  151. }
  152. }