字节数组与二进制、十六进制相互转换

复制代码
常用的一些转换方法,整理如下,在编程中,用得比较多的是十进制跟十六进制,二进制跟八进制用的很少。

复制代码
namespace Jerry.Framework.Socket.Common
{
    public class Utility
    {
        /// <summary>
        /// 将指定进制的字符串转换到指定进制的字符串
        /// </summary>
        /// <param name="value">要转换的字符串</param>
        /// <param name="fromBase">value 中数字的基数,它必须是 2、8、10 或 16。</param>
        /// <param name="toBase">value 中数字的基数,它必须是 2、8、10 或 16。</param>
        /// <returns></returns>
        public static string ToString(string value, int fromBase, int toBase)
        {
            return Convert.ToString(Convert.ToInt32(value, fromBase), toBase);
        }

        /// <summary>
        /// 将字节数组节转换成指定进制的字符串
        /// </summary>
        /// <param name="byteArray">要转换的字符串</param>
        /// <param name="toBase">它必须是 2 或 16</param>
        /// <returns></returns>
        public static string ToString(byte[] byteArray, int toBase = 16)
        {
            if (byteArray == null || byteArray.Length == 0)
                return string.Empty;

            switch (toBase)
            {
                case 2:
                    return ByteArrayToBinaryString(byteArray);
                case 16:
                    return ByteArrayToHexStr(byteArray);
                default:
                    return string.Empty;
            }
        }

        /// <summary>
        /// 将指定进制的字符串转换成字节数组
        /// </summary>
        /// <param name="value"></param>
        /// <param name="fromBase"></param>
        /// <returns></returns>
        public static byte[] ToByteArray(string value, int fromBase = 16)
        {
            if (string.IsNullOrEmpty(value))
                return null;
            switch (fromBase)
            {
                case 2:
                    return BinaryStringToByteArray(value);
                case 16:
                    return HexStrToByteArray(value);
                default:
                    return null;
            }
        }

        /// <summary>
        /// 字节数组节转换成二进制字符串
        /// </summary>
        /// <param name="b">要转换的字节数组</param>
        /// <returns></returns>
        private static string ByteArrayToBinaryString(byte[] byteArray)
        {
            int capacity = byteArray.Length * 8;
            StringBuilder sb = new StringBuilder(capacity);
            for (int i = 0; i < byteArray.Length; i++)
            {
                sb.Append(byte2BinString(byteArray[i]));
            }
            return sb.ToString();
        }

        private static string byte2BinString(byte b)
        {
            return Convert.ToString(b, 2).PadLeft(8, '0');
        }

        /// <summary>
        /// 二进制字符串转换成字节数组
        /// </summary>
        /// <param name="binaryString">要转换的字符,如"00000000 11111111"</param>
        /// <returns></returns>
        private static byte[] BinaryStringToByteArray(string binaryString)
        {
            binaryString = binaryString.Replace(" ", "");

            if ((binaryString.Length % 8) != 0)
                throw new ArgumentException("二进制字符串长度不对");

            byte[] buffer = new byte[binaryString.Length / 8];
            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = Convert.ToByte(binaryString.Substring(i * 8, 8).Trim(), 2);
            }
            return buffer;

        }

        /// <summary>
        /// 字节数组转换成十六进制字符串
        /// </summary>
        /// <param name="bytes">要转换的字节数组</param>
        /// <returns></returns>
        private static string ByteArrayToHexStr(byte[] byteArray)
        {
            int capacity = byteArray.Length * 2;
            StringBuilder sb = new StringBuilder(capacity);

            if (byteArray != null)
            {
                for (int i = 0; i < byteArray.Length; i++)
                {
                    sb.Append(byteArray[i].ToString("X2"));
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// 十六进制字符串转换成字节数组 
        /// </summary>
        /// <param name="hexString">要转换的字符串</param>
        /// <returns></returns>
        private static byte[] HexStrToByteArray(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                throw new ArgumentException("十六进制字符串长度不对");
            byte[] buffer = new byte[hexString.Length / 2];
            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Trim(), 0x10);
            }
            return buffer;
        }
    }
}
复制代码

 

 
复制代码
posted @   supperwu  阅读(27991)  评论(7编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示