1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- using System;
- using System.ComponentModel;
- using System.Reflection;
- namespace WayneChina_IcCardReader_SinoChem
- {
- public static class Extensions
- {
- public static void SetPropertyValue(this object target, string propertyName, string propertyValue)
- {
- PropertyInfo propInfo = target.GetType().GetProperty(propertyName);
- Type propType = propInfo.PropertyType;
- if (propType.IsGenericType && propType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
- {
- if (propertyValue == null)
- {
- propInfo.SetValue(target, null, null);
- return;
- }
- propType = new NullableConverter(propInfo.PropertyType).UnderlyingType;
- }
- propInfo.SetValue(target, Convert.ChangeType(propertyValue, propType), null);
- }
- public static byte[] Crc16Ccitt(this byte[] bytes)
- {
- const ushort poly = 4129;
- ushort[] table = new ushort[256];
- ushort initialValue = 0xFFFF;
- ushort temp, a;
- ushort crc = initialValue;
- for (int i = 0; i < table.Length; ++i)
- {
- temp = 0;
- a = (ushort)(i << 8);
- for (int j = 0; j < 8; ++j)
- {
- if (((temp ^ a) & 0x8000) != 0)
- temp = (ushort)((temp << 1) ^ poly);
- else
- temp <<= 1;
- a <<= 1;
- }
- table[i] = temp;
- }
- for (int i = 0; i < bytes.Length; ++i)
- {
- crc = (ushort)((crc << 8) ^ table[((crc >> 8) ^ (0xFF & bytes[i]))]);
- }
- return BitConverter.GetBytes(crc);
- }
- }
- }
|