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


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

    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;
            }
        }
    }
    
    
    
     
  • 相关阅读:
    金蝶 kis一些功能的内部逻辑和个人技巧
    mysql横向和纵向合并sql数据用于展示,快递导出导入海量数据
    laravel carbon时间处理组件开发文档-中文版
    金蝶kis数据库说明(转载)
    java微信支付,对账单下载
    RabbitMQ中文文档PHP版本(七)--发布者确认
    RabbitMQ中文文档PHP版本(六)--远程过程调用(RPC)
    RabbitMQ中文文档PHP版本(五)--主题
    RabbitMQ中文文档PHP版本(四)--路由
    RabbitMQ中文文档PHP版本(二)--发布/订阅
  • 原文地址:https://www.cnblogs.com/supperwu/p/2512877.html
Copyright © 2020-2023  润新知