zoukankan      html  css  js  c++  java
  • 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);
        }
    
    }
    
    参考

    补码

  • 相关阅读:
    Node post请求 通常配合ajax
    Node json
    Node params和query的Get请求传参
    Node express
    java NIO FileChannel
    IO 输出 PrintStream和PrintWriter
    ByteBuffer
    分析dump
    oracle free space
    SHELL 在指定行的前/后插入指定内容
  • 原文地址:https://www.cnblogs.com/lxyit/p/9463586.html
Copyright © 2011-2022 走看看