bytes in ByteBuffer To Hex - Java java.nio

Java examples for java.nio:ByteBuffer

Description

bytes in ByteBuffer To Hex

Demo Code


//package com.java2s;
import java.nio.ByteBuffer;

public class Main {
    private final static char[] hexArray = "0123456789ABCDEF".toCharArray();

    public static String bytesToHex(ByteBuffer bytes) {
        char[] hexChars = new char[bytes.limit() * 2];
        for (int j = 0; j < bytes.limit(); j++) {
            int v = bytes.get() & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }/* w  w  w . j a v  a 2s  . com*/
        return new String(hexChars);
    }
}

Related Tutorials