1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- using System;
- using System.Globalization;
- using System.Text;
- namespace Dfs.WayneChina.HengshanPayTerminal
- {
- 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);
- }
- }
- }
|