Java ByteBuffer Read readHexString(ByteBuffer buffer, int nrBytes)

Here you can find the source of readHexString(ByteBuffer buffer, int nrBytes)

Description

read Hex String

License

Apache License

Declaration

public static String readHexString(ByteBuffer buffer, int nrBytes) 

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 readHexString(ByteBuffer buffer, int nrBytes) {
        byte[] bytes = new byte[nrBytes];
        for (int i = bytes.length - 1; i >= 0; i--) {
            bytes[i] = buffer.get();/*  ww  w  . j a  v  a  2  s  .c o m*/
        }
        return toHexString(bytes);
    }

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

    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. readFixedPoint88(ByteBuffer bb)
  2. readFourByteInt(ByteBuffer buffer, int start)
  3. readFrom(File file, ByteBuffer buffer)
  4. readFully(final FileChannel src, final ByteBuffer dst, final long position)
  5. readHealthFloat16(ByteBuffer data)
  6. readIso639(ByteBuffer bb)
  7. readKatakana(ByteBuffer b, char s[], int off, int len)
  8. readLen(ByteBuffer dup, int nls)
  9. readLink(ByteBuffer bb)