Guest User

Untitled

a guest
Mar 21st, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.18 KB | None | 0 0
  1. #region Copyright(c) 1998-2013, Arnaud Colin Licence GNU GPL version 3
  2. /* Copyright(c) 1998-2013, Arnaud Colin
  3. * All rights reserved.
  4. *
  5. * Licence GNU GPL version 3
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * -> Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * -> Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. */
  17. #endregion
  18.  
  19. using System;
  20. using System.Collections.Generic;
  21. #if NET35
  22. using System.Linq;
  23. #endif
  24. using System.Runtime.CompilerServices;
  25. using System.Text;
  26. #if NET45
  27. using System.Threading.Tasks;
  28. #endif
  29.  
  30. namespace Conversion
  31. {
  32.  
  33. /// <summary>
  34. ///
  35. /// <example> </example>///
  36. /// </summary>
  37. public class ByteToChar
  38. {
  39.  
  40. /// <summary>
  41. /// <para>
  42. /// <param name="buf">byte[]</param>
  43. /// <param name="Start">int</param>
  44. /// <param name="length">int?</param>
  45. /// </para>
  46. /// convert the Byte[] to char[]
  47. /// <example> ByteToString.GetArray("0104",0,4); </example>///
  48. /// <returns>Byte[]</returns>
  49. /// </summary>
  50. #if !WindowsCE
  51. public static string GetArray(byte[] buf, int Start = 0, int? length = null)
  52. #else
  53. public static string GetArray( byte[] buf , int Start, int? length)
  54. #endif
  55.  
  56. {
  57. int? _Length = null;
  58.  
  59.  
  60. if(length.HasValue)
  61. {
  62. if( length <= buf.Length)
  63. _Length = length;
  64. else
  65. _Length = buf.Length;
  66. }
  67. else
  68. _Length = buf.Length;
  69.  
  70. if (!_Length.HasValue)
  71. return null;
  72.  
  73. string stemp = string.Empty;
  74. //char[] ArrChar = new char[_Length.Value];
  75.  
  76. int count = 0;
  77. for (int i = Start; i < _Length.Value; i++)
  78. {
  79. stemp += (char)buf[i];
  80. count++;
  81. }
  82.  
  83. return stemp;
  84. }
  85. }
  86.  
  87.  
  88. /// <summary>
  89. /// Class for convert IpAddress
  90. /// <example> IPAddress.LongToString(); IPAddress.StringToLong("192.168.1.1"); </example>///
  91. /// </summary>
  92. public class IPAddress : StringAndLong
  93. {
  94. /// <summary>
  95. /// <para>
  96. /// <param name="val">long</param>
  97. /// </para>
  98. /// convert the long in string
  99. /// <example> IPAddress.LongToString(); </example>///
  100. /// <returns>string</returns>
  101. /// </summary>
  102. #if NET45
  103. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  104. #endif
  105. public static string LongToString(long val)
  106. {
  107.  
  108. return LongToString(val, 4, '.', 10);
  109.  
  110. }
  111.  
  112. /// <summary>
  113. /// <para>
  114. /// <param name="val">string</param>
  115. /// </para>
  116. /// convert the string in long
  117. /// <example> IPAddress.StringToLong("192.168.1.1") </example>///
  118. /// <returns>long</returns>
  119. /// </summary>
  120. #if NET45
  121. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  122. #endif
  123. public static long StringToLong(string val)
  124. {
  125. return StringToLong(val, '.', 10);
  126. }
  127. }
  128.  
  129. /// <summary>
  130. /// Class for convert MacAddress
  131. /// <example> MacAddress.StringToLong("00:17:23:14:32:75:00:00"); </example>///
  132. /// </summary>
  133. public class MacAddress : StringAndLong
  134. {
  135. /// <summary>
  136. /// <para>
  137. /// <param name="val">long</param>
  138. /// </para>
  139. /// convert the long in string
  140. /// <example> MacAddress.LongToString(); </example>///
  141. /// <returns>string</returns>
  142. /// </summary>
  143. #if NET45
  144. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  145. #endif
  146. public static string LongToString(long val)
  147. {
  148. return LongToString(val, 8, ':', 16);
  149. }
  150.  
  151. /// <summary>
  152. /// <para>
  153. /// <param name="val">string</param>
  154. /// </para>
  155. /// convert the string in long
  156. /// <example> MacAddress.StringToLong("00:17:23:14:32:75:00:00") </example>///
  157. /// <returns>long</returns>
  158. /// </summary>
  159. #if NET45
  160. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  161. #endif
  162. public static long StringToLong(string val)
  163. {
  164. return StringToLong(val, ':', 16);
  165. }
  166. }
  167.  
  168. /// <summary>
  169. /// Class convert string to long and long to string
  170. /// </summary>
  171. public class StringAndLong
  172. {
  173. /// <summary>
  174. /// <para>
  175. /// <param name="val">string</param>
  176. /// <param name="Count">int</param>
  177. /// <param name="sp">char</param>
  178. /// <param name="ibase">int</param>
  179. /// </para>
  180. /// convert the long in string
  181. /// <example> sample.LongToString(val, 8, ':', 16); </example>///
  182. /// <returns>string</returns>
  183. /// </summary>
  184. public static string LongToString(long val, int Count, char sp, int ibase)
  185. {
  186. long mask = 0xFF;
  187. string ip = string.Empty;
  188. string format = string.Empty;
  189.  
  190. for (int i = 0; i < Count; i++)
  191. {
  192.  
  193. long vt1 = (mask << (8 * i));
  194. byte num = (byte)((val & vt1) >> (8 * i));
  195.  
  196.  
  197. if (ibase == 16)
  198. format = "X2";
  199. else
  200. format = "d";
  201.  
  202.  
  203. if (i == 0)
  204.  
  205. ip = num.ToString(format);
  206.  
  207. else
  208.  
  209. ip = ip + sp + num.ToString(format);
  210.  
  211. }
  212.  
  213. return ip;
  214.  
  215. }
  216.  
  217. /// <summary>
  218. /// <para>
  219. /// <param name="val">string</param>
  220. /// <param name="sp">char</param>
  221. /// <param name="ibase">int</param>
  222. /// </para>
  223. /// convert the string in long
  224. /// <example> StringToLong(val, ':', 16); </example>///
  225. /// <returns>long</returns>
  226. /// </summary>
  227. public static long StringToLong(string val, char sp, int ibase)
  228. {
  229. string[] ipBytes;
  230. long num = 0;
  231.  
  232. if (!string.IsNullOrEmpty(val))
  233. {
  234.  
  235. ipBytes = val.Split(sp);
  236.  
  237. for (int i = 0; i < ipBytes.Length; i++)
  238. {
  239. long vt1 = (long)Convert.ToByte(ipBytes[i],(int) ibase);
  240. int vt2 = (int)(8 * i);
  241. num += (vt1 << vt2);
  242. }
  243.  
  244. }
  245. return (long)num;
  246. }
  247.  
  248.  
  249. public static long StringToLong(byte[] val)
  250. {
  251. // string[] ipBytes;
  252. long num = 0;
  253.  
  254. // if (!string.IsNullOrEmpty(val))
  255. {
  256.  
  257. // ipBytes = val.Split(sp);
  258.  
  259. for (int i = 0; i < val.Length; i++)
  260. {
  261. long vt1 = (long)val[i];
  262. int vt2 = (int)(8 * i);
  263. num += (vt1 << vt2);
  264. }
  265.  
  266. }
  267. return (long)num;
  268. }
  269.  
  270. }
  271. }
Add Comment
Please, Sign In to add comment