using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HengshanPaymentTerminal { public static class HengshanExtensions { /// <summary> /// Converts a byte array, normally two bytes to an integer. /// e.g. byte array of 0x00 0x12 is converted to integer of 18. /// </summary> /// <param name="source">The source byte array</param> /// <returns>integer value</returns> public static int ToInt32(this byte[] source) { var hexTarget = new StringBuilder(source.Length * 2); foreach (byte b in source) hexTarget.AppendFormat("{0:x2}", b); return int.Parse(hexTarget.ToString(), NumberStyles.HexNumber); } /// <summary> /// Converts a char array to an unsigned short integer. /// </summary> /// <param name="source">The source char array.</param> /// <returns>An unsigned short integer value.</returns> public static ushort ToUInt16(this char[] source) { var hexTarget = new StringBuilder(source.Length * 2); foreach (byte b in source) hexTarget.AppendFormat("{0:x2}", b); return UInt16.Parse(hexTarget.ToString(), NumberStyles.HexNumber); } public static byte ToByte(this string source) { return Convert.ToByte(Int16.Parse(source, NumberStyles.HexNumber)); } public static ushort ToUInt16(this string source) { return UInt16.Parse(source, NumberStyles.HexNumber); } public static uint ToUInt32(this string source) { return UInt32.Parse(source, NumberStyles.HexNumber); } } }