java 10进制转16进制带ox格式输出

摘自https://yq.aliyun.com/wenzhang/show_28512

java 10进制转16进制带ox格式输出

如 60(10进制) 输出 : 0x00,0x00,0x00,0x3c

解决方案

如下代码:

public static void main(String[] args) throws IOException {
        int num=60;
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        DataOutputStream out=new DataOutputStream(baos);
        out.writeInt(num);
        byte[] bs=baos.toByteArray();
        for(byte b:bs) {
            System.out.print("0x");
            System.out.print(Integer.toString(b>>4&0xF,16).toUpperCase());
            System.out.print(Integer.toString(b&0xF,16).toUpperCase());
            System.out.print(" ");
        }
    }

 

猜你喜欢

转载自357029540.iteye.com/blog/2384492