码迷,mamicode.com
首页 > Windows程序 > 详细

C# Byte[] 转String 无损转换

时间:2016-03-14 18:54:57      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:

C# Byte[] 转String 无损转换

转载请注明出处 http://www.cnblogs.com/Huerye/

 

 /// <summary>
        /// string 转成byte[]
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns>byte[]</returns>
        private byte[] strToToHexByte(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>
        /// byte[] 转string
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns>string</returns>
        public string byteToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                }
            }
            return returnStr;
        }

 

  

 

C# Byte[] 转String 无损转换

标签:

原文地址:http://www.cnblogs.com/Huerye/p/5276585.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!