Extend.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using LitJson;
  2. using System;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using UnityEngine;
  6. namespace WS
  7. {
  8. ///<summary>扩展</summary>
  9. public static class Extend
  10. {
  11. public static string ToHex(this byte b)
  12. {
  13. return b.ToString("X2");
  14. }
  15. public static string ToHex(this byte[] bytes)
  16. {
  17. StringBuilder stringBuilder = new StringBuilder();
  18. foreach (byte b in bytes)
  19. {
  20. stringBuilder.Append(b.ToString("X2"));
  21. }
  22. return stringBuilder.ToString();
  23. }
  24. public static string ToHex(this byte[] bytes, string format)
  25. {
  26. StringBuilder stringBuilder = new StringBuilder();
  27. foreach (byte b in bytes)
  28. {
  29. stringBuilder.Append(b.ToString(format));
  30. }
  31. return stringBuilder.ToString();
  32. }
  33. public static string ToHex(this byte[] bytes, int offset, int count)
  34. {
  35. StringBuilder stringBuilder = new StringBuilder();
  36. for (int i = offset; i < offset + count; ++i)
  37. {
  38. stringBuilder.Append(bytes[i].ToString("X2"));
  39. }
  40. return stringBuilder.ToString();
  41. }
  42. public static string ToStr(this byte[] bytes)
  43. {
  44. return Encoding.Default.GetString(bytes);
  45. }
  46. public static string ToStr(this byte[] bytes, int index, int count)
  47. {
  48. return Encoding.Default.GetString(bytes, index, count);
  49. }
  50. public static string Utf8ToStr(this byte[] bytes)
  51. {
  52. return Encoding.UTF8.GetString(bytes);
  53. }
  54. public static string Utf8ToStr(this byte[] bytes, int index, int count)
  55. {
  56. return Encoding.UTF8.GetString(bytes, index, count);
  57. }
  58. public static void WriteTo(this byte[] bytes, int offset, uint num)
  59. {
  60. bytes[offset] = (byte)(num & 0xff);
  61. bytes[offset + 1] = (byte)((num & 0xff00) >> 8);
  62. bytes[offset + 2] = (byte)((num & 0xff0000) >> 16);
  63. bytes[offset + 3] = (byte)((num & 0xff000000) >> 24);
  64. }
  65. public static void WriteTo(this byte[] bytes, int offset, int num)
  66. {
  67. bytes[offset] = (byte)(num & 0xff);
  68. bytes[offset + 1] = (byte)((num & 0xff00) >> 8);
  69. bytes[offset + 2] = (byte)((num & 0xff0000) >> 16);
  70. bytes[offset + 3] = (byte)((num & 0xff000000) >> 24);
  71. }
  72. public static void WriteTo(this byte[] bytes, int offset, byte num)
  73. {
  74. bytes[offset] = num;
  75. }
  76. public static void WriteTo(this byte[] bytes, int offset, short num)
  77. {
  78. bytes[offset] = (byte)(num & 0xff);
  79. bytes[offset + 1] = (byte)((num & 0xff00) >> 8);
  80. }
  81. public static void WriteTo(this byte[] bytes, int offset, ushort num)
  82. {
  83. bytes[offset] = (byte)(num & 0xff);
  84. bytes[offset + 1] = (byte)((num & 0xff00) >> 8);
  85. }
  86. /// <summary>转换为16进制</summary>
  87. /// <param name="lowerCase">是否小写</param>
  88. public static string ToHex(this byte[] bytes, bool lowerCase = true)
  89. {
  90. if (bytes == null)
  91. return null;
  92. var result = new StringBuilder();
  93. var format = lowerCase ? "x2" : "X2";
  94. for (var i = 0; i < bytes.Length; i++)
  95. {
  96. result.Append(bytes[i].ToString(format));
  97. }
  98. return result.ToString();
  99. }
  100. /// <summary>16进制转字节数组</summary>
  101. public static byte[] HexToBytes(this string s)
  102. {
  103. if (string.IsNullOrEmpty(s))
  104. return null;
  105. var bytes = new byte[s.Length / 2];
  106. for (int x = 0; x < s.Length / 2; x++)
  107. {
  108. int i = (Convert.ToInt32(s.Substring(x * 2, 2), 16));
  109. bytes[x] = (byte)i;
  110. }
  111. return bytes;
  112. }
  113. public static byte[] ToByteArray(this string str)
  114. {
  115. byte[] byteArray = Encoding.Default.GetBytes(str);
  116. return byteArray;
  117. }
  118. public static byte[] ToUtf8(this string str)
  119. {
  120. byte[] byteArray = Encoding.UTF8.GetBytes(str);
  121. return byteArray;
  122. }
  123. /// <summary>转换为Base64</summary>
  124. public static string ToBase64(this byte[] bytes)
  125. {
  126. if (bytes == null)
  127. return null;
  128. return Convert.ToBase64String(bytes);
  129. }
  130. private readonly static StringBuilder sb = new StringBuilder();
  131. /// <summary>设置输入框限制字符</summary>
  132. /// <param name="arg0">输入字符</param>
  133. /// <param name="regex">正则表达式</param>
  134. /// <param name="length">限制长度</param>
  135. public static string SetRegexText(string arg0, string regex, int length)
  136. {
  137. sb.Clear();
  138. if (arg0.Length >= length)
  139. {
  140. arg0 = arg0.Substring(0, arg0.Length - 1);
  141. }
  142. Regex reg = new Regex(regex);
  143. if (!reg.IsMatch(arg0))
  144. {
  145. char[] chararr = arg0.ToCharArray();
  146. for (int i = 0; i < chararr.Length; i++)
  147. {
  148. string str = chararr[i].ToString();
  149. if (reg.IsMatch(str))
  150. {
  151. sb.Append(str);
  152. }
  153. }
  154. arg0 = sb.ToString();
  155. }
  156. return arg0;
  157. }
  158. public static string ToJson(this object obj)
  159. {
  160. return JsonMapper.ToJson(obj);
  161. }
  162. public static bool BelongsToHierarchy(this Transform subject, Transform hierarchy)
  163. {
  164. if (subject == null)
  165. return false;
  166. else if (subject == hierarchy)
  167. return true;
  168. else
  169. return BelongsToHierarchy(subject.transform.parent, hierarchy);
  170. }
  171. }
  172. }