ByteArrayConvert.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Text;
  3. namespace Wit.SDK.Utils
  4. {
  5. /// <summary>
  6. /// 字节数组转换工具类
  7. /// </summary>
  8. public class ByteArrayConvert
  9. {
  10. /// <summary>
  11. /// byte数组转string
  12. /// </summary>
  13. /// <param name="data"></param>
  14. /// <returns></returns>
  15. public static string ByteArrayToString(byte[] data)
  16. {
  17. return Encoding.Default.GetString(data);
  18. }
  19. /// <summary>
  20. /// string转 byte数组
  21. /// </summary>
  22. /// <param name="data"></param>
  23. /// <returns></returns>
  24. public static byte[] StringToByteArray(string data)
  25. {
  26. return Encoding.Default.GetBytes(data);
  27. }
  28. /// <summary>
  29. /// byte数组转16进制字符串
  30. /// </summary>
  31. /// <param name="data"></param>
  32. /// <returns></returns>
  33. public static string ByteArrayToHexString(byte[] data)
  34. {
  35. if (data == null) { return ""; }
  36. StringBuilder builder = new StringBuilder();
  37. for (int i = 0; i < data.Length; i++)
  38. {
  39. builder.Append(string.Format("{0:X2} ", data[i]));
  40. }
  41. return builder.ToString().Trim();
  42. }
  43. /// <summary>
  44. /// 16进制字符串转byte数组
  45. /// </summary>
  46. /// <param name="data"></param>
  47. /// <returns></returns>
  48. public static byte[] HexStringToByteArray(string data)
  49. {
  50. string[] chars = data.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  51. byte[] returnBytes = new byte[chars.Length];
  52. //逐个字符变为16进制字节数据
  53. for (int i = 0; i < chars.Length; i++)
  54. {
  55. returnBytes[i] = Convert.ToByte(chars[i], 16);
  56. }
  57. return returnBytes;
  58. }
  59. /// <summary>
  60. /// byte数组转10进制字符串
  61. /// </summary>
  62. /// <param name="data"></param>
  63. /// <returns></returns>
  64. public static string ByteArrayToDecString(byte[] data)
  65. {
  66. StringBuilder builder = new StringBuilder();
  67. for (int i = 0; i < data.Length; i++)
  68. {
  69. builder.Append(data[i] + " ");
  70. }
  71. return builder.ToString().Trim();
  72. }
  73. /// <summary>
  74. /// 10进制字符串转byte数组
  75. /// </summary>
  76. /// <param name="data"></param>
  77. /// <returns></returns>
  78. public static byte[] DecStringToByteArray(string data)
  79. {
  80. string[] chars = data.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  81. byte[] returnBytes = new byte[chars.Length];
  82. //逐个字符变为10进制字节数据
  83. for (int i = 0; i < chars.Length; i++)
  84. {
  85. returnBytes[i] = Convert.ToByte(chars[i], 10);
  86. }
  87. return returnBytes;
  88. }
  89. /// <summary>
  90. /// byte数组转八进制字符串
  91. /// </summary>
  92. /// <param name="data"></param>
  93. /// <returns></returns>
  94. public static string ByteArrayToOtcString(byte[] data)
  95. {
  96. StringBuilder builder = new StringBuilder();
  97. for (int i = 0; i < data.Length; i++)
  98. {
  99. builder.Append(Convert.ToString(data[i], 8) + " ");
  100. }
  101. return builder.ToString().Trim();
  102. }
  103. /// <summary>
  104. /// 八进制字符串转byte数组
  105. /// </summary>
  106. /// <param name="data"></param>
  107. /// <returns></returns>
  108. public static byte[] OtcStringToByteArray(string data)
  109. {
  110. string[] chars = data.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  111. byte[] returnBytes = new byte[chars.Length];
  112. //逐个字符变为8进制字节数据
  113. for (int i = 0; i < chars.Length; i++)
  114. {
  115. returnBytes[i] = Convert.ToByte(chars[i], 8);
  116. }
  117. return returnBytes;
  118. }
  119. /// <summary>
  120. /// 二进制字符串转byte数组
  121. /// </summary>
  122. /// <param name="data"></param>
  123. /// <returns></returns>
  124. public static byte[] BinStringToByteArray(string data)
  125. {
  126. string[] chars = data.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  127. byte[] returnBytes = new byte[chars.Length];
  128. //逐个字符变为2进制字节数据
  129. for (int i = 0; i < chars.Length; i++)
  130. {
  131. returnBytes[i] = Convert.ToByte(chars[i], 2);
  132. }
  133. return returnBytes;
  134. }
  135. /// <summary>
  136. /// byte数组转二进制字符串
  137. /// </summary>
  138. /// <param name="data"></param>
  139. /// <returns></returns>
  140. public static string ByteArrayToBinString(byte[] data)
  141. {
  142. StringBuilder builder = new StringBuilder();
  143. for (int i = 0; i < data.Length; i++)
  144. {
  145. builder.Append(Convert.ToString(data[i], 2) + " ");
  146. }
  147. return builder.ToString().Trim();
  148. }
  149. }
  150. }