zoukankan      html  css  js  c++  java
  • 一个格式化输出二进制数据的工具

        public static String byteToHexString(byte[] arr) {
            StringBuffer sb = new StringBuffer(arr.length * 2);
            sb.append("00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15    text
    ");
            sb.append("-------------------------------------------------------
    ");
            
            for (int i = 0; i < arr.length; i++) {
                String hex = Integer.toHexString(0xFF & arr[i]).toUpperCase();
                if (hex.length() == 1) {
                    hex = "0" + hex;
                }
                sb.append(hex).append(' ');
                if ((i + 1) % 16 == 0) {
                    //TODO
                    sb.append('
    ');
                }
            }
            sb.append('
    ');
            return sb.toString();
        }

    测试:

    import java.math.BigInteger;
    import java.util.BitSet;
    
    
    /**
     * 测试bitset 的异或操作能不能正确的的看出两个bigInteger的bit 位之间的差异
     * @author leo
     *
     */
    public class TestBitSetAndBigInteger {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            BigInteger i1 = new BigInteger("1020");
            BigInteger i2 = new BigInteger("1021");
            
            BigInteger i3 = new BigInteger("10131036798906733839");
            BigInteger i4 = new BigInteger("10131036798906734863");//i3 + 1024
            
            BitSet s1 = BitSet.valueOf(i1.toByteArray());
            BitSet s2 = BitSet.valueOf(i2.toByteArray());
            
            BitSet s3 = BitSet.valueOf(i3.toByteArray());
            BitSet s4 = BitSet.valueOf(i4.toByteArray());
            
            System.out.println("i1:::::
    " + Utils.byteToHexString(i1.toByteArray()));
            System.out.println("s1:::::
    " + Utils.byteToHexString(s1.toByteArray()));
            
            System.out.println("i2:::::
    " + Utils.byteToHexString(i2.toByteArray()));
            System.out.println("s2:::::
    " + Utils.byteToHexString(s2.toByteArray()));
            
            System.out.println("i3:::::
    " + Utils.byteToHexString(i3.toByteArray()));
            System.out.println("s3:::::
    " + Utils.byteToHexString(s3.toByteArray()));
            
            System.out.println("i4:::::
    " + Utils.byteToHexString(i4.toByteArray()));
            System.out.println("s4:::::
    " + Utils.byteToHexString(s4.toByteArray()));
            
            BitSet s11 = (BitSet) s1.clone();
            s11.xor(s2);
            BitSet s22 = (BitSet) s2.clone();
            s22.xor(s1);
            System.out.println("s1 xor s2 = " + s11.cardinality());
            System.out.println("s2 xor s1 = " + s22.c
    i1:::::
    00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15    text
    -------------------------------------------------------
    03 FC 
    
    s1:::::
    00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15    text
    -------------------------------------------------------
    03 FC 
    
    i2:::::
    00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15    text
    -------------------------------------------------------
    03 FD 
    
    s2:::::
    00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15    text
    -------------------------------------------------------
    03 FD 
    
    i3:::::
    00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15    text
    -------------------------------------------------------
    00 8C 98 AC 4A C5 3F 15 0F 
    
    s3:::::
    00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15    text
    -------------------------------------------------------
    00 8C 98 AC 4A C5 3F 15 0F 
    
    i4:::::
    00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15    text
    -------------------------------------------------------
    00 8C 98 AC 4A C5 3F 19 0F 
    
    s4:::::
    00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15    text
    -------------------------------------------------------
    00 8C 98 AC 4A C5 3F 19 0F 
    
    s1 xor s2 = 1
    s2 xor s1 = 1
    s3 xor s4 = 2
    s4 xor s3 = 2
  • 相关阅读:
    GIS的发展
    ajax请求头加Token时发生的跨域(CORS)请求问题
    js 给定时间,如'20130830',换算和今天的天数差
    过程改进点滴需求调研经验之一
    昨天向PAM推荐的好书
    关于专人整理和分析需求
    走出开发混沌
    过程改进点滴需求调研经验之二
    代码重构注意事项
    插件框架内核完成
  • 原文地址:https://www.cnblogs.com/zdcin/p/3184353.html
Copyright © 2011-2022 走看看