Here you can find the source of toHex(ByteBuffer buf)
Parameter | Description |
---|---|
buf | The buffer to interpret, using the data between its position and limit. |
public static String toHex(ByteBuffer buf)
//package com.java2s; /**/*from w w w. j ava 2 s .c o m*/ * Copyright (C) 2011 Darien Hager * * This code is part of the "HL2Parse" project, and is licensed under * a Creative Commons Attribution-ShareAlike 3.0 Unported License. For * either a summary of conditions or the full legal text, please visit: * * http://creativecommons.org/licenses/by-sa/3.0/ * * Permissions beyond the scope of this license may be available * at http://technofovea.com/ . */ import java.nio.ByteBuffer; public class Main { static final char[] HEX_CHARS = "0123456789ABCDEF".toCharArray(); /** * Given an array of bytes, return a string of hexadecimal characters. * @param bytes The byte array to interpret * @return A series of (upper-cased) hexadecimal characters, without any leading values such as 0x */ public static String toHex(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (byte b : bytes) { sb.append(HEX_CHARS[(b & 0xF0) >>> 4]); sb.append(HEX_CHARS[b & 0x0F]); } return sb.toString(); } public static String toHex(byte b) { return toHex(new byte[] { b }); } /** * Interprets the bytes inside the given buffer as a hexadecimal string. * * The data in the buffer between its position and limit will be used, and the position will * be restored to its original value afterwards. * * @param buf The buffer to interpret, using the data between its position and limit. * @return A string of (upper-cased) hexadecimal, with no leading prefix (ex. 0x) */ public static String toHex(ByteBuffer buf) { int orig = buf.position(); StringBuilder sb = new StringBuilder(); while (buf.remaining() > 0) { byte b = buf.get(); sb.append(HEX_CHARS[(b & 0xF0) >>> 4]); sb.append(HEX_CHARS[b & 0x0F]); } buf.position(orig); return sb.toString(); } }