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
  • 相关阅读:
    Asp.net弹出浏览器客户端确认对话框代码 Carlwave
    VS 2005 与SQL Server 2005整合优势在哪里?(from csdn.net) Carlwave
    如何让搜索引擎收录我的站点 Carlwave
    超强扩展性的DNNDotNetNuke模块功能分类列表(from 中国DNN) Carlwave
    DotNetNuke命名空间概述 Carlwave
    Most Popular Questions and Answers on ASP.NET Whidbey(from asp.net forums,write by ASP.NET Team) Carlwave
    火箭官方宣告麦蒂缺阵五周 季后赛前景蒙上阴影 Carlwave
    asp.net有效使用缓存(转) Carlwave
    《Business Rules Engine Overview》《业务规则引擎概述》write by Mark Kamoski Carlwave
    中国详细省市县自治区名称列表(含access数据库和sql2000备份数据库) Carlwave
  • 原文地址:https://www.cnblogs.com/zdcin/p/3184353.html
Copyright © 2011-2022 走看看