读卡器 C# 辅助进制转换函数


public static class CardReaderExt
{
    /// <summary>
    /// Byte[] to String
    /// </summary>
    /// <param name="bytes"></param>
    /// <returns></returns>
    public static string ToHexString(this byte[] bytes)
    {
        return System.Text.Encoding.ASCII.GetString(bytes);
    }

    /// <summary>
    /// String to byte[]
    /// </summary>
    /// <param name="asciiString"></param>
    /// <returns></returns>
    public static byte[] ToHexArray(this string asciiString)
    {
        return System.Text.Encoding.ASCII.GetBytes(asciiString);
    }

    /// <summary>
    /// Hex String to byte[]
    /// 16 进制字符串转为 Byte 数组
    /// </summary>
    /// <param name="asciiString"></param>
    /// <param name="number"></param>
    /// <returns></returns>
    public static byte[] ToByteArray(this string asciiString, int number)
    {
        List<byte> result = new List<byte>();
        //int index = 0;
        //int length = asciiString.Length % 2 == 0 ? asciiString.Length / 2 : asciiString.Length / 2 + 1;
        //while (index < length)
        //{
        //    if (index == length - 1 && asciiString.Length % 2 == 1)
        //    {
        //        number = 1;
        //    }
        //    string temp = asciiString.Substring(index * number, number);
        //    result.Add(Convert.ToByte(temp, 16));
        //    index++;
        //}
        Queue<char> queue = new Queue<char>(asciiString.ToArray().ToList());
        string temp = string.Empty;
        while (queue.Count > 0)
        {
            if (queue.Count == 1)
                temp = queue.Dequeue().ToString();
            else
                temp = queue.Dequeue().ToString() + queue.Dequeue().ToString();
            result.Add(Convert.ToByte(temp, 16));
        }

        return result.ToArray();
    }

    /// <summary>
    /// byte[] to HexString
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public static string ToHex(this byte[] data)
    {
        return ToHex(data, "");
    }

    /// <summary>
    /// byte[] to HexString
    /// </summary>
    /// <param name="HexValue"></param>
    /// <returns></returns>
    public static string HexToString(this string HexValue)
    {
        string StrValue = "";
        while (HexValue.Length > 0)
        {
            StrValue += System.Convert.ToChar(System.Convert.ToUInt32(HexValue.Substring(0, 2), 16)).ToString();
            HexValue = HexValue.Substring(2, HexValue.Length - 2);
        }
        return StrValue;
    }

    /// <summary>
    /// byte[] to HexString
    /// </summary>
    /// <param name="data"></param>
    /// <param name="prefix"></param>
    /// <returns></returns>
    public static string ToHex(this byte[] data, string prefix)
    {
        char[] lookup = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        int i = 0, p = prefix.Length, l = data.Length;
        char[] c = new char[l * 2 + p];
        byte d;
        for (; i < p; ++i) c[i] = prefix[i];
        i = -1;
        --l;
        --p;
        while (i < l)
        {
            d = data[++i];
            c[++p] = lookup[d / 0x10];
            c[++p] = lookup[d % 0x10];
        }
        return new string(c, 0, c.Length);
    }

}

知识共享许可协议
《读卡器 C# 辅助进制转换函数》 常伟华 创作。
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议 | 3.0 中国大陆许可协议进行许可。

站内公告

A PHP Error was encountered

Severity: Core Warning

Message: PHP Startup: zip: Unable to initialize module Module compiled with module API=20060613 PHP compiled with module API=20090626 These options need to match

Filename: Unknown

Line Number: 0

Backtrace: