Guest User

Untitled

a guest
Mar 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. /// <summary>
  2. /// Class for convert string to array of bytes
  3. /// <example> </example>///
  4. /// </summary>
  5. public static class ByteToString
  6. {
  7.  
  8. #if NET45
  9. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  10. #endif
  11. private static byte calcul(char x, char y)
  12. {
  13. int h = 0;
  14. int l = 0;
  15. if (x >= 'a')
  16. h = (x - 'a') + 10;
  17. else
  18. h = (x - '0');
  19.  
  20. if (y >= 'a')
  21. l = (y - 'a') + 10;
  22. else
  23. l = (y - '0');
  24.  
  25. return (byte)((h << 4) + l);
  26. }
  27.  
  28.  
  29.  
  30. #if NET45
  31. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  32. #endif
  33. #if !WindowsCE
  34. private static int? ValidIndex(int Size, bool NeedOd, int Start = 0, int? length = null)
  35. #else
  36. private static int? ValidIndex(int Size, bool NeedOd, int Start, int? length)
  37. #endif
  38. {
  39. int _Length;
  40.  
  41. if (length.HasValue)
  42. if (Start > Size || (NeedOd && (length % 2 != 0)) || (Size - Start - length.Value) <= 0)
  43. return null;
  44. else
  45. _Length = length.Value;
  46. else
  47. if (Start > Size || ((_Length = (Size - Start)) <= 0) || (NeedOd && (_Length % 2 != 0)))
  48. return null;
  49.  
  50. return _Length;
  51. }
  52.  
  53.  
  54.  
  55. /// <summary>
  56. /// <para>
  57. /// <param name="chaine">string</param>
  58. /// <param name="Start">int</param>
  59. /// <param name="length">int?</param>
  60. /// </para>
  61. /// convert the string in Byte[]
  62. /// <example> ByteToString.GetArray("0104",0,4); </example>///
  63. /// <returns>Byte[]</returns>
  64. /// </summary>
  65. #if !WindowsCE
  66. public static byte[] GetArray(string chaine, int Start = 0, int? length = null)
  67. #else
  68. public static byte[] GetArray(string chaine, int Start , int? length )
  69. #endif
  70.  
  71. {
  72. int? _Length = null;
  73.  
  74. if ((_Length = ValidIndex(chaine.Length, true, Start, length)) == null)
  75. return null;
  76.  
  77. byte[] buf = new byte[_Length.Value / 2];
  78.  
  79. int count = 0;
  80. for (int i = Start; i < _Length.Value; i = i + 2)
  81. {
  82. buf[count] = calcul(chaine[i], chaine[i + 1]);
  83. count++;
  84. }
  85.  
  86. return buf;
  87. }
  88.  
  89.  
  90. /// <summary>
  91. /// <para>
  92. /// <param name="chaine">string</param>
  93. /// </para>
  94. /// convert the string in Byte[]
  95. /// <example> ByteToString.GetArray("0104"); </example>///
  96. /// <returns>Byte[]</returns>
  97. /// </summary>
  98. public static byte[] GetArray(string chaine)
  99. {
  100. return GetArray(chaine, 0, null);
  101. }
  102.  
  103.  
  104. /// <summary>
  105. /// <para>
  106. /// <param name="buf">string</param>
  107. /// <param name="Start">int</param>
  108. /// <param name="length">int?</param>
  109. /// </para>
  110. /// convert a Byte[] in string
  111. /// <example> ByteToString.GetString(new byte() { 0x01, 0x04 }, 0 , null ); </example>///
  112. /// <returns>string</returns>
  113. /// </summary>
  114. #if !WindowsCE
  115. public static string GetString(this byte[] buf, int Start = 0, int? length = null)
  116. #else
  117. public static string GetString(byte[] buf, int Start, int? length )
  118. #endif
  119.  
  120. {
  121. int? _Length = null;
  122.  
  123. if ((_Length = ValidIndex(buf.Length, false, Start, length)) == null)
  124. return null;
  125.  
  126. string res = string.Empty;
  127.  
  128. for (int i = Start; i < _Length.Value; i++)
  129. {
  130. res += buf[i].ToString("x2");
  131. }
  132. return res;
  133.  
  134. }
  135.  
  136.  
  137. /// <summary>
  138. /// <para>
  139. /// <param name="buf">string</param>
  140. /// </para>
  141. /// convert a Byte[] in string
  142. /// <example> ByteToString.GetString(new byte() { 0x01, 0x04 }); </example>///
  143. /// <returns>string</returns>
  144. /// </summary>
  145. public static string GetString(byte[] buf)
  146. {
  147. string res = string.Empty;
  148.  
  149. for (int i = 0; i < buf.Length; i++)
  150. {
  151. res += buf[i].ToString("x2");
  152. }
  153. return res;
  154. }
  155.  
  156.  
  157. /// <summary>
  158. /// <para>
  159. /// <param name="buf">string</param>
  160. /// <param name="length">int?</param>
  161. /// </para>
  162. /// convert a Byte[] in string
  163. /// <example> ByteToString.GetString(new byte() { 0x01, 0x04 }, 2 ); </example>///
  164. /// <returns>string</returns>
  165. /// </summary>
  166. public static string GetString(byte[] buf, int length)
  167. {
  168. return GetString(buf, 0, length);
  169. }
  170.  
  171. }
Add Comment
Please, Sign In to add comment