ExtendsUtil.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using System.Text;
  6. namespace WCS.Data
  7. {
  8. public class ExtendsUtil
  9. {
  10. /// <summary>
  11. /// byte[]解码转字符串(扫描条码解码)
  12. /// </summary>
  13. /// <param name="barcodeBytes"></param>
  14. /// <returns></returns>
  15. public static string GetBarCodeStr(byte[] barcodeBytes)
  16. {
  17. string strbarcodeBytes = string.Empty;
  18. if (barcodeBytes[0] != 0)
  19. {
  20. for (int i = 0; i < barcodeBytes.Length; i++)
  21. {
  22. //if (barcodeBytes[i] == 13) break;
  23. var asciiTochar = new ASCIIEncoding();
  24. char[] ascii = asciiTochar.GetChars(barcodeBytes);
  25. strbarcodeBytes = strbarcodeBytes + ascii[i];
  26. }
  27. }
  28. return strbarcodeBytes;
  29. }
  30. /// <summary>
  31. /// byte[]解码转字符串(扫描条码解码)
  32. /// </summary>
  33. /// <param name="barcodeBytes"></param>
  34. /// <returns></returns>
  35. public static string GetBarCodeStr2(byte[] barcodeBytes)
  36. {
  37. string strbarcodeBytes = string.Empty;
  38. for (int i = 0; i < barcodeBytes.Length; i++)
  39. {
  40. //if (barcodeBytes[i] == 13) break;
  41. var asciiTochar = new ASCIIEncoding();
  42. char[] ascii = asciiTochar.GetChars(barcodeBytes);
  43. strbarcodeBytes = strbarcodeBytes + ascii[i];
  44. }
  45. return strbarcodeBytes;
  46. }
  47. /// <summary>
  48. /// ushort转换成Byte
  49. /// </summary>
  50. /// <param name="value">需转换的值</param>
  51. /// <param name="startIndex">存放值的索引</param>
  52. public static byte[] UshortToByte(ushort value)
  53. {
  54. byte[] from_wcs = System.BitConverter.GetBytes(value);
  55. byte[] tempdb = new byte[2];
  56. tempdb[0] = from_wcs[1];//地位值
  57. tempdb[1] = from_wcs[0];//高位值
  58. return tempdb;
  59. }
  60. /// <summary>
  61. /// Bit转byte
  62. /// </summary>
  63. /// <param name="bit"></param>
  64. /// <returns></returns>
  65. public static byte BitToByte(BitArray bit)
  66. {
  67. byte[] res = new byte[1];
  68. for (int i = 0; i < bit.Count; i++)
  69. {
  70. bit.CopyTo(res, 0);
  71. }
  72. return res[0];
  73. }
  74. /// <summary>
  75. /// Bit转Int
  76. /// </summary>
  77. /// <param name="bit"></param>
  78. /// <returns></returns>
  79. public static int BitToInt(BitArray bit)
  80. {
  81. int[] res = new int[1];
  82. for (int i = 0; i < bit.Count; i++)
  83. {
  84. bit.CopyTo(res, 0);
  85. }
  86. return res[0];
  87. }
  88. public static int BitToInt2(BitArray bit)
  89. {
  90. int res = 0;
  91. for (int i = bit.Count - 1; i >= 0; i--)
  92. {
  93. res = bit[i] ? res + (1 << i) : res;
  94. }
  95. return res;
  96. }
  97. /// <summary>
  98. /// uint转换成Byte
  99. /// </summary>
  100. /// <param name="value">需转换的值</param>
  101. /// <param name="startIndex">存放值的索引</param>
  102. //public static byte[] UintToByte(uint value)
  103. //{
  104. // byte[] from_wcs = System.BitConverter.GetBytes(value);
  105. // byte[] tempdb = new byte[2];
  106. // tempdb[0] = from_wcs[3];//地位值
  107. // tempdb[1] = from_wcs[2];//高位值
  108. // tempdb[0] = from_wcs[1];//地位值
  109. // tempdb[1] = from_wcs[0];//高位值
  110. // return tempdb;
  111. //}
  112. /// <summary>
  113. /// uint转换成Byte
  114. /// </summary>
  115. /// <param name="value">需转换的值</param>
  116. /// <param name="startIndex">存放值的索引</param>
  117. public static byte[] UintToByte(uint value)
  118. {
  119. byte[] from_wcs = System.BitConverter.GetBytes(value);
  120. byte[] tempdb = new byte[4];
  121. tempdb[0] = from_wcs[3];//地位值
  122. tempdb[1] = from_wcs[2];//高位值
  123. tempdb[2] = from_wcs[1];//地位值
  124. tempdb[3] = from_wcs[0];//高位值
  125. return tempdb;
  126. }
  127. /// <summary>
  128. /// double转换成Byte
  129. /// </summary>
  130. /// <param name="value">需转换的值</param>
  131. /// <param name="startIndex">存放值的索引</param>
  132. public static byte[] FloatToByte(float value)
  133. {
  134. byte[] from_wcs = System.BitConverter.GetBytes(value);
  135. byte[] tempdb = new byte[4];
  136. tempdb[0] = from_wcs[3];//地位值
  137. tempdb[1] = from_wcs[2];//高位值
  138. tempdb[2] = from_wcs[1];//地位值
  139. tempdb[3] = from_wcs[0];//高位值
  140. return tempdb;
  141. }
  142. #region 转换指定字节数组为字符串
  143. /// <summary>
  144. /// 转换指定字节数组为字符串
  145. /// </summary>
  146. /// <param name="ByteGet">字节数组Byte[]</param>
  147. /// <param name="myEncoding">编码方式</param>
  148. /// <returns></returns>
  149. private static string getStringFromByteArray(Byte[] ByteGet, Encoding myEncoding)
  150. {
  151. int i, lngCount;
  152. StringBuilder aTemp = new StringBuilder(10000);
  153. lngCount = ByteGet.Length;
  154. for (i = 0; i < lngCount; i += 10000)
  155. {
  156. aTemp.Append(myEncoding.GetString(ByteGet, i, (lngCount >= i + 10000 ? 10000 : lngCount - i)));
  157. }
  158. if (i <= lngCount)
  159. {
  160. aTemp.Append(myEncoding.GetString(ByteGet, i, (lngCount - i)));
  161. }
  162. return aTemp.ToString();
  163. }
  164. #endregion
  165. public static D Mapper<D, S>(S s)
  166. {
  167. D d = Activator.CreateInstance<D>();
  168. try
  169. {
  170. var sType = s.GetType();
  171. var dType = typeof(D);
  172. foreach (PropertyInfo sP in sType.GetProperties())
  173. {
  174. foreach (PropertyInfo dP in dType.GetProperties())
  175. {
  176. if (dP.Name == sP.Name)
  177. {
  178. dP.SetValue(d, sP.GetValue(s));
  179. break;
  180. }
  181. }
  182. }
  183. }
  184. catch (Exception ex)
  185. {
  186. }
  187. return d;
  188. }
  189. }
  190. }