2015年3月4日 星期三

C# HexString to ByteArray

16進位的字串轉成ByteArray

namespace ConsoleApplication1
{
    using System;
    using System.Linq;

    class Program
    {
        static void Main(string[] args)
        {
            string hexstring = "0123456789abcdef";
            byte[] byteArray = Enumerable.Range(0, hexstring.Length)
                .Where(x => x % 2 == 0)
                .Select(x => Convert.ToByte(hexstring.Substring(x, 2), 16))
                .ToArray();
            Console.WriteLine(BitConverter.ToString(byteArray));
        }
    }
}