Java ByteBuffer to Hex toHexString(ByteBuffer byteBuffer)

Here you can find the source of toHexString(ByteBuffer byteBuffer)

Description

to Hex String

License

Apache License

Declaration

public static String toHexString(ByteBuffer byteBuffer) 

Method Source Code

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

import java.nio.ByteBuffer;

public class Main {
    private static final char[] HEX = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
            'F' };

    public static String toHexString(byte value) {
        return new String(new char[] { HEX[(value >>> 4) & 0xf], HEX[(value >>> 0) & 0xf] });
    }/*from w  ww. j  a  v a  2s.co  m*/

    public static String toHexString(byte[] bytes) {
        return toHexString(bytes, 0, bytes.length, 0);
    }

    public static String toHexString(byte[] bytes, int offset, int length, int spacing) {
        StringBuilder sb = new StringBuilder();
        for (int ix = offset; ix < offset + length; ix++) {
            sb.append(HEX[(bytes[ix] >>> 4) & 0xf]);
            sb.append(HEX[bytes[ix] & 0xf]);
            if (spacing > 0 && ix % spacing == spacing - 1 && ix < offset + length - 1) {
                sb.append(' ');
            }
        }
        return sb.toString();
    }

    public static String toHexString(ByteBuffer byteBuffer) {
        byteBuffer.rewind();
        return toHexString(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(),
                byteBuffer.remaining(), 4);
    }

    public static String toHexString(int value) {
        return new String(new char[] { HEX[(value >>> 28) & 0xf], HEX[(value >>> 24) & 0xf],
                HEX[(value >>> 20) & 0xf], HEX[(value >>> 16) & 0xf], HEX[(value >>> 12) & 0xf],
                HEX[(value >>> 8) & 0xf], HEX[(value >>> 4) & 0xf], HEX[(value >>> 0) & 0xf] });
    }

    public static String toHexString(short value) {
        return new String(new char[] { HEX[(value >>> 12) & 0xf], HEX[(value >>> 8) & 0xf],
                HEX[(value >>> 4) & 0xf], HEX[(value >>> 0) & 0xf] });
    }
}

Related

  1. toHexString(ByteBuffer bb, boolean withSpaces)
  2. toHexString(ByteBuffer buffer)
  3. toHexString(ByteBuffer buffer)
  4. toHexString(ByteBuffer buffer, int size)
  5. toHexString(ByteBuffer byteBuffer)
  6. toHexString(final ByteBuffer buffer)
  7. toHexValue(ByteBuffer buffer)