一个格式化输出二进制数据的工具

    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\n");
        sb.append("-------------------------------------------------------\n");
        
        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('\n');
            }
        }
        sb.append('\n');
        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:::::\n" + Utils.byteToHexString(i1.toByteArray()));
        System.out.println("s1:::::\n" + Utils.byteToHexString(s1.toByteArray()));
        
        System.out.println("i2:::::\n" + Utils.byteToHexString(i2.toByteArray()));
        System.out.println("s2:::::\n" + Utils.byteToHexString(s2.toByteArray()));
        
        System.out.println("i3:::::\n" + Utils.byteToHexString(i3.toByteArray()));
        System.out.println("s3:::::\n" + Utils.byteToHexString(s3.toByteArray()));
        
        System.out.println("i4:::::\n" + Utils.byteToHexString(i4.toByteArray()));
        System.out.println("s4:::::\n" + 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

 

转载于:https://www.cnblogs.com/zdcin/p/3184353.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值