Java Convert via ByteBuffer hexDump(byte[] buffer)

Here you can find the source of hexDump(byte[] buffer)

Description

hex Dump

License

Apache License

Declaration

public static String hexDump(byte[] buffer) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.nio.ByteBuffer;

public class Main {
    static final String HEX_VALUES = "0123456789ABCDEF";

    public static String hexDump(byte[] buffer) {
        StringBuilder buf = new StringBuilder(buffer.length << 1);
        for (byte b : buffer)
            addHexByte(buf, b);//  w ww  .  j a  v  a2 s.  c om

        return buf.toString();
    }

    public static String hexDump(ByteBuffer buffer) {
        byte[] data = new byte[buffer.remaining()];
        int pos = buffer.position();
        buffer.get(data);
        buffer.position(pos);
        StringBuilder buf = new StringBuilder(buffer.remaining() + 22);
        for (byte b : data)
            addHexByte(buf, b);

        return buf.toString();
    }

    private static void addHexByte(StringBuilder buf, byte b) {
        buf.append(HEX_VALUES.charAt((b & 0xF0) >> 4)).append(HEX_VALUES.charAt((b & 0x0F)));
    }
}

Related

  1. hexDump(byte... b)
  2. hexStringToBytes(String hexString)
  3. hexStrToStr(String hexStr)
  4. hexToAscii(byte[] src)
  5. hexToBytes(String hexStr)