string 和 byte[] 互轉換工具

string 和 byte[] 互轉換工具


    /// <summary>
    /// string 和 byte[] 互轉換工具
    /// </summary>
    public sealed class StringConvert
    {
        /// <summary>
        /// 字符串轉換爲byte數組
        /// </summary>
        /// <param name="inputString">傳入字符串參數</param>
        /// <returns>byte[]</returns>
        public static byte[] StringConvertToBytes(string inputString)
        {
            return System.Convert.FromBase64String(inputString);
        }

        /// <summary>
        /// byte數組轉換爲字符串
        /// </summary>
        /// <param name="inputBytes">傳入byte數組參數</param>
        /// <returns>string</returns>
        public static string BytesConvertToString(byte[] inputBytes)
        {
            return System.Convert.ToBase64String(inputBytes);
        }

        /// <summary>
        /// 16進制字符串轉16進制字節
        /// </summary>
        /// <param name="hexString">16進制字符數據</param>
        /// <returns>字節數據</returns>
        public static byte[] HexStrToByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
            {
                hexString += " ";
            }
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
            {
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            }
            return returnBytes;
        }

        /// <summary>
        /// 16進制字節轉16進制字符串
        /// </summary>
        /// <param name="bytes">數據</param>
        /// <returns>16進制字符串</returns>
        public static string ByteToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                }
            }
            return returnStr;
        }

        /// <summary>
        /// 數字或16進制字符串轉BCD編碼
        /// </summary>
        /// <param name="strTemp">數據</param>
        /// <returns>BCD編碼後的數據</returns>
        public static Byte[] ConvertFrom(string strTemp)
        {
            try
            {
                if (Convert.ToBoolean(strTemp.Length & 1))//數字的二進制碼最後1位是1則爲奇數
                {
                    strTemp = "0" + strTemp;//數位爲奇數時前面補0
                }
                Byte[] aryTemp = new Byte[strTemp.Length / 2];
                for (int i = 0; i < (strTemp.Length / 2); i++)
                {
                    char high = (char)Convert.ToByte(strTemp.Substring(i * 2, 1), 16);
                    char low = (char)Convert.ToByte(strTemp.Substring(i * 2 + 1, 1), 16);
                    aryTemp[i] = (Byte)((high << 4) | low);
                }
                return aryTemp;//高位在前
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// BCD碼轉換16進制(壓縮BCD)
        /// </summary>
        /// <param name="strTemp">數據</param>
        /// <param name="len">數據長度</param>
        /// <returns>BCD壓縮後的數據</returns>
        public static Byte[] ConvertFrom(string strTemp, int len)
        {
            try
            {
                Byte[] Temp = ConvertFrom(strTemp.Trim());
                Byte[] return_Byte = new Byte[len];
                if (len != 0)
                {
                    if (Temp.Length < len)
                    {
                        for (int i = 0; i < len - Temp.Length; i++)
                        {
                            return_Byte[i] = 0x00;
                        }
                    }
                    Array.Copy(Temp, 0, return_Byte, len - Temp.Length, Temp.Length);
                    return return_Byte;
                }
                else
                {
                    return Temp;
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 16進制轉換BCD(解壓BCD)
        /// </summary>
        /// <param name="aData">數據</param>
        /// <returns>BCD解壓後數據</returns>
        public static string ConvertTo(byte[] aData)
        {
            try
            {
                StringBuilder sb = new StringBuilder(aData.Length * 2);
                foreach (Byte b in aData)
                {
                    sb.Append(Convert.ToString(b >> 4, 16));
                    sb.Append(Convert.ToString(b & 0x0f, 16));
                }
                return sb.ToString();
            }
            catch
            {
                return null;
            }
        }

    }




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章