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

namespace Dispatch
{
    internal class CHex2String
    {
        internal static byte[] HexString2Byte(string msg)
        {
            msg = msg.Replace(" ", "");//移除空格

            //create a byte array the length of the
            //divided by 2 (Hex is 2 characters in length)
            byte[] comBuffer = new byte[msg.Length / 2];
            for (int i = 0; i < msg.Length; i += 2)
            {
                //convert each set of 2 characters to a byte and add to the array
                comBuffer[i / 2] = (byte)Convert.ToByte(msg.Substring(i, 2), 16);
            }

            return comBuffer;
        }
        internal static int[] StringArrayToIntArray(string[] msg)
        {
            int[] comBuffer = new int[msg.Length];
            for (int i = 0; i < msg.Length; i ++)
            {
                comBuffer[i] = Convert.ToInt32(msg[i]);
            }

            return comBuffer;
        }
        internal static string Byte2HexString(byte[] comByte, int len)
        {
            StringBuilder builder = new StringBuilder(len * 3);
            /*foreach (byte data in comByte)
            {
                builder.Append(Convert.ToString(data, 16).PadLeft(2, '0').PadRight(3, ' '));
            }*/
            for (int i = 0; i < len; i++)
            {
                builder.Append(Convert.ToString(comByte[i], 16).PadLeft(2, '0').PadRight(3, ' '));
            }
            return builder.ToString().ToUpper();
        }
    }
}

