• Java基础03 byte[] 与 16进制字符串之间的转换


    说明:

    Java中byte用二进制表示占用8位;
    16进制的每个字符需要用4位二进制位来表示;
    可以把每个byte转换成两个相应的16进制字符;
    即把byte的高4位和低4位分别转换成相应的16进制字符H和L;
    并组合起来得到byte转换到16进制字符串;
    
    相反的转换也是将两个16进制字符转换成一个byte;
    
    

    16进制字符串转byte[]

    // 16进制字符串转byte[]
        public static byte[] hexStringToBytes(String hexString) {
            if (hexString == null || "".equals(hexString)) {
                return null;
            }
            hexString = hexString.toUpperCase();
            int length = hexString.length() / 2;
            char[] hexChars = hexString.toCharArray();
            byte[] d = new byte[length];
            for (int i = 0; i < length; i++) {
                int pos = i * 2;
                d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
            }
            return d;
        }
    
        private static byte charToByte(char c) {
            return (byte) "0123456789ABCDEF".indexOf(c);
        }
    

    byte[] 转16进制

    public static String bytesToHexString(byte[] src){
            if (src == null || src.length <= 0) {
                return null;
            }
            StringBuilder stringBuilder = new StringBuilder("");
            for (byte b : src) {
                int v = b & 0xFF;
                String hv = Integer.toHexString(v);
                if (hv.length() < 2) {
                    stringBuilder.append(0);
                }
                stringBuilder.append(hv);
            }
            return stringBuilder.toString();
        }
    

    16进制字符串与byte[]转换

    static final byte[] HEX_TABLE = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
    static final char[] HEX_CHAR_TABLE = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    
    // byte[] 转16进制
    public static String byte2HexString(byte[] data) {
            if (data == null || data.length == 0) {
                return null;
            }
            byte[] hex = new byte[data.length * 2];
            int index = 0;
            for (byte b : data) {
                int v = b & 255;
                hex[index++] = (byte) HEX_CHAR_TABLE[v >>> 4];
                hex[index++] = (byte) HEX_CHAR_TABLE[v & 15];
            }
            return new String(hex);
        }
        // 16进制转byte[]
        public static byte[] hex2Byte(String hexString) {
            if (hexString == null || hexString.length() == 0) {
                return null;
            }
            hexString = hexString.toUpperCase();
            if (hexString.length() % 2 != 0) {
                throw new RuntimeException();
            }
    
            byte[] data = new byte[hexString.length() / 2];
            char[] chars = hexString.toCharArray();
    
            for (int i = 0; i < hexString.length(); i += 2) {
                data[i / 2] = (byte) (HEX_TABLE[getHexCharValue(chars[i])] << 4 | HEX_TABLE[getHexCharValue(chars[i + 1])]);
            }
            return data;
        }
    
    private static int getHexCharValue(char c) {
            int index = 0;
            for (char c1 : HEX_CHAR_TABLE) {
                if (c == c1) {
                    return index;
                }
                ++index;
            }
            return 0;
        }
    
    
    通过知识/经验的分享,节省开发者的时间.
  • 相关阅读:
    Redis学习笔记-安装篇(Centos7)
    图片上传预览方式,了解下?
    Angular中依赖注入方式的几种写法
    Javascript实现打开或退出浏览器全屏
    从头开始学Web开发—CSS_01
    JavaScript DOM知识 (一)
    javascript中的scroll事件
    javascript中继承的实现
    认识Javascript中的作用域和作用域链
    javascript中的闭包
  • 原文地址:https://www.cnblogs.com/ysloong/p/15564184.html
Copyright © 2020-2023  润新知