using System; using System.Collections; using System.Collections.Generic; using System.Reflection; using System.Text; namespace WCS.Data { public class ExtendsUtil { /// /// byte[]解码转字符串(扫描条码解码) /// /// /// public static string GetBarCodeStr(byte[] barcodeBytes) { string strbarcodeBytes = string.Empty; if (barcodeBytes[0] != 0) { for (int i = 0; i < barcodeBytes.Length; i++) { //if (barcodeBytes[i] == 13) break; var asciiTochar = new ASCIIEncoding(); char[] ascii = asciiTochar.GetChars(barcodeBytes); strbarcodeBytes = strbarcodeBytes + ascii[i]; } } return strbarcodeBytes; } /// /// byte[]解码转字符串(扫描条码解码) /// /// /// public static string GetBarCodeStr2(byte[] barcodeBytes) { string strbarcodeBytes = string.Empty; for (int i = 0; i < barcodeBytes.Length; i++) { //if (barcodeBytes[i] == 13) break; var asciiTochar = new ASCIIEncoding(); char[] ascii = asciiTochar.GetChars(barcodeBytes); strbarcodeBytes = strbarcodeBytes + ascii[i]; } return strbarcodeBytes; } /// /// ushort转换成Byte /// /// 需转换的值 /// 存放值的索引 public static byte[] UshortToByte(ushort value) { byte[] from_wcs = System.BitConverter.GetBytes(value); byte[] tempdb = new byte[2]; tempdb[0] = from_wcs[1];//地位值 tempdb[1] = from_wcs[0];//高位值 return tempdb; } /// /// Bit转byte /// /// /// public static byte BitToByte(BitArray bit) { byte[] res = new byte[1]; for (int i = 0; i < bit.Count; i++) { bit.CopyTo(res, 0); } return res[0]; } /// /// Bit转Int /// /// /// public static int BitToInt(BitArray bit) { int[] res = new int[1]; for (int i = 0; i < bit.Count; i++) { bit.CopyTo(res, 0); } return res[0]; } public static int BitToInt2(BitArray bit) { int res = 0; for (int i = bit.Count - 1; i >= 0; i--) { res = bit[i] ? res + (1 << i) : res; } return res; } /// /// uint转换成Byte /// /// 需转换的值 /// 存放值的索引 //public static byte[] UintToByte(uint value) //{ // byte[] from_wcs = System.BitConverter.GetBytes(value); // byte[] tempdb = new byte[2]; // tempdb[0] = from_wcs[3];//地位值 // tempdb[1] = from_wcs[2];//高位值 // tempdb[0] = from_wcs[1];//地位值 // tempdb[1] = from_wcs[0];//高位值 // return tempdb; //} /// /// uint转换成Byte /// /// 需转换的值 /// 存放值的索引 public static byte[] UintToByte(uint value) { byte[] from_wcs = System.BitConverter.GetBytes(value); byte[] tempdb = new byte[4]; tempdb[0] = from_wcs[3];//地位值 tempdb[1] = from_wcs[2];//高位值 tempdb[2] = from_wcs[1];//地位值 tempdb[3] = from_wcs[0];//高位值 return tempdb; } /// /// double转换成Byte /// /// 需转换的值 /// 存放值的索引 public static byte[] FloatToByte(float value) { byte[] from_wcs = System.BitConverter.GetBytes(value); byte[] tempdb = new byte[4]; tempdb[0] = from_wcs[3];//地位值 tempdb[1] = from_wcs[2];//高位值 tempdb[2] = from_wcs[1];//地位值 tempdb[3] = from_wcs[0];//高位值 return tempdb; } #region 转换指定字节数组为字符串 /// /// 转换指定字节数组为字符串 /// /// 字节数组Byte[] /// 编码方式 /// private static string getStringFromByteArray(Byte[] ByteGet, Encoding myEncoding) { int i, lngCount; StringBuilder aTemp = new StringBuilder(10000); lngCount = ByteGet.Length; for (i = 0; i < lngCount; i += 10000) { aTemp.Append(myEncoding.GetString(ByteGet, i, (lngCount >= i + 10000 ? 10000 : lngCount - i))); } if (i <= lngCount) { aTemp.Append(myEncoding.GetString(ByteGet, i, (lngCount - i))); } return aTemp.ToString(); } #endregion public static D Mapper(S s) { D d = Activator.CreateInstance(); try { var sType = s.GetType(); var dType = typeof(D); foreach (PropertyInfo sP in sType.GetProperties()) { foreach (PropertyInfo dP in dType.GetProperties()) { if (dP.Name == sP.Name) { dP.SetValue(d, sP.GetValue(s)); break; } } } } catch (Exception ex) { } return d; } } }