一个格局化输出二进制数据的对象

    添加时间:2013-7-11 点击量:

        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);

    forint i = 0; i < arr.length; i++) {
    String hex
    = Integer.toHexString(0 xFF & 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


    容易发怒的意思就是: 别人做了蠢事, 然后我们代替他们, 表现出笨蛋的样子。—— 蔡康永
    分享到: