using System; using System.Globalization; using System.Text; namespace Dfs.WayneChina.HengshanPayTerminal { public static class HengshanExtensions { /// /// Converts a byte array, normally two bytes to an integer. /// e.g. byte array of 0x00 0x12 is converted to integer of 18. /// /// The source byte array /// integer value 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); } /// /// Converts a char array to an unsigned short integer. /// /// The source char array. /// An unsigned short integer value. 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); } } }