ByteBuffer to Hex String - Java java.nio

Java examples for java.nio:ByteBuffer String

Description

ByteBuffer to Hex String

Demo Code


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

public class Main {
    public static String toHex(ByteBuffer bb) {
        StringBuilder sb = new StringBuilder("[ ");
        while (bb.hasRemaining()) {
            sb.append(String.format("%02X ", bb.get()));
        }//from  w w  w.  j  a va  2  s .  c  o  m
        sb.append("]");
        return sb.toString();
    }

    public static String toString(ByteBuffer bb) {
        StringBuilder sb = new StringBuilder();
        while (bb.hasRemaining()) {
            sb.append((char) bb.get());
        }
        return sb.toString();
    }
}

Related Tutorials