123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Text;
- namespace WCS.Data
- {
- public class ExtendsUtil
- {
- /// <summary>
- /// byte[]解码转字符串(扫描条码解码)
- /// </summary>
- /// <param name="barcodeBytes"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// byte[]解码转字符串(扫描条码解码)
- /// </summary>
- /// <param name="barcodeBytes"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// ushort转换成Byte
- /// </summary>
- /// <param name="value">需转换的值</param>
- /// <param name="startIndex">存放值的索引</param>
- 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;
- }
- /// <summary>
- /// Bit转byte
- /// </summary>
- /// <param name="bit"></param>
- /// <returns></returns>
- 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];
- }
- /// <summary>
- /// Bit转Int
- /// </summary>
- /// <param name="bit"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// uint转换成Byte
- /// </summary>
- /// <param name="value">需转换的值</param>
- /// <param name="startIndex">存放值的索引</param>
- //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;
- //}
- /// <summary>
- /// uint转换成Byte
- /// </summary>
- /// <param name="value">需转换的值</param>
- /// <param name="startIndex">存放值的索引</param>
- 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;
- }
- /// <summary>
- /// double转换成Byte
- /// </summary>
- /// <param name="value">需转换的值</param>
- /// <param name="startIndex">存放值的索引</param>
- 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 转换指定字节数组为字符串
- /// <summary>
- /// 转换指定字节数组为字符串
- /// </summary>
- /// <param name="ByteGet">字节数组Byte[]</param>
- /// <param name="myEncoding">编码方式</param>
- /// <returns></returns>
- 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<D, S>(S s)
- {
- D d = Activator.CreateInstance<D>();
- 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;
- }
- }
- }
|