• Java中转换为十六进制的几种实现


    public class HexUtil {
    
        private static final String[] DIGITS_UPPER =
                {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
    
        public static void main(String[] args) throws DecoderException {
    
            System.out.println(toHex1((byte) -128));
            System.out.println(toHex2((byte) -128));
            System.out.println(toHex3((byte) -128));
            System.out.println(toHex4((byte) -128));
        }
    
    
        public static String toHex1(byte value) {
            int high = (value & 0xF0) >>> 4;
            int low = value & 0x0F;
            return DIGITS_UPPER[high] + DIGITS_UPPER[low];
        }
    
        public static String toHex2(byte value) {
            int high = (value >>> 4) & 0x0F;
            int low = value & 0x0F;
            return DIGITS_UPPER[high] + DIGITS_UPPER[low];
        }
    
    
        public static String toHex3(byte value) {
            int tmp = value;
            if (value < 0) {
                tmp = value + 256;
            }
            int high = tmp / 16;
            int low = tmp % 16;
            return DIGITS_UPPER[high] + DIGITS_UPPER[low];
        }
    
        public static String toHex4(byte value) {
            return String.format("%x", value);
        }
    
    }
    
    参考

    补码

  • 相关阅读:
    Peer code review
    分析图书管理系统的5W1H
    项目风险分析作业
    课堂练习
    功能分析四个象限
    Android需求分析作业
    电梯演说模板练习
    敏捷流程的理解
    团队模型的小组辩论
    结对编程任意Demo
  • 原文地址:https://www.cnblogs.com/lxyit/p/9463586.html
Copyright © 2020-2023  润新知