using System;
using System.Collections.Generic;
using System.Text;

namespace MifareOneSample
{
    class HexEdit
    {
        //
        public static string SetWindowTextEx(byte[] pText, byte nLen)
        {
            if (pText == null) return "";
            string strEdit = "";

            for (int i = 0; i < nLen; i++)
            {
                Int16 wAscII = SetHexToAscII((byte)(pText[i]));
                //strEdit += wAscII.ToString("X");   
                ASCIIEncoding ascii = new ASCIIEncoding();
                byte[] AscII = new byte[2];
                AscII[0] = (byte)(wAscII & 0xff);
                AscII[1] = (byte)(wAscII >> 8);
                strEdit += ascii.GetString(AscII,0,2);
            }
            return strEdit;
        }
        //MAKEWORD
        public static Int16 MAKEWORD(byte hiByte, byte loByte)
        {
            return (Int16)(((Int16)((byte)(hiByte & 0xff))) | ((Int16)((byte)(loByte & 0xff) << 8)));
        }
        //HexToAscII
        public static Int16 SetHexToAscII(byte szHex)
        {
            Int16 wAscII;
            byte loBits = (byte)(szHex & 0x0f);
            byte hiBits = (byte)((szHex & 0xf0) >> 4);

            byte loByte, hiByte;

            if (loBits <= 9)
                loByte = (byte)(loBits + 0x30);
            else loByte = (byte)(loBits + 0x37);

            if (hiBits <= 9)
                hiByte = (byte)(hiBits + 0x30);
            else hiByte = (byte)(hiBits + 0x37);
            wAscII = MAKEWORD(hiByte, loByte);
            return wAscII;
        }
        //HexToBytes
        public static byte[] HexToBytes(string str)
        {
            byte[] bytes = new byte[str.Length / 2];
            char A;
            byte LoByte = 0;
            byte HiByte = 0;
            for (int i = 0; i < str.Length; i++)
            {
                A = str[i];
                if ((i + 1) % 2 != 0)
                    LoByte = (byte)(ASCIIToHex(A) << 4);
                else
                    HiByte = (byte)(ASCIIToHex(A) & 0xf);
                bytes[i / 2] = (byte)(LoByte | HiByte);
            }
            return bytes;
        }
        //ASCIIToHex
        private static byte ASCIIToHex(char A)
        {
            byte Byte = 0x00;
            switch (A)
            {
                case '0': Byte = 0x0;
                    break;
                case '1': Byte = 0x1;
                    break;
                case '2': Byte = 0x2;
                    break;
                case '3': Byte = 0x3;
                    break;
                case '4': Byte = 0x4;
                    break;
                case '5': Byte = 0x5;
                    break;
                case '6': Byte = 0x6;
                    break;
                case '7': Byte = 0x7;
                    break;
                case '8': Byte = 0x8;
                    break;
                case '9': Byte = 0x9;
                    break;
                case 'A':
                case 'a': Byte = 0xa;
                    break;
                case 'B':
                case 'b': Byte = 0xb;
                    break;
                case 'C':
                case 'c': Byte = 0xc;
                    break;
                case 'D':
                case 'd': Byte = 0xd;
                    break;
                case 'E':
                case 'e': Byte = 0xe;
                    break;
                case 'F':
                case 'f': Byte = 0xf;
                    break;

            }
            return Byte;
        }
    }
}
