`
357029540
  • 浏览: 725495 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

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

    博客分类:
  • JAVA
阅读更多

摘自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(" ");
        }
    }

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics