Java ByteBuffer to Hex toHexString(ByteBuffer bb)

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

Description

to Hex String

License

Open Source License

Declaration

public static String toHexString(ByteBuffer bb) 

Method Source Code


//package com.java2s;
/*/*from  w w w.  j a  v  a 2 s .  c o  m*/
 * ***** BEGIN LICENSE BLOCK *****
 * Zimbra Collaboration Suite Server
 * Copyright (C) 2007, 2009, 2010, 2011, 2013, 2014, 2016 Synacor, Inc.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software Foundation,
 * version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with this program.
 * If not, see <https://www.gnu.org/licenses/>.
 * ***** END LICENSE BLOCK *****
 */

import java.nio.ByteBuffer;

public class Main {
    public static String toHexString(ByteBuffer bb) {
        return appendHex(new StringBuilder(), bb).toString();
    }

    public static StringBuilder appendHex(StringBuilder sb, ByteBuffer bb) {
        int limit = bb.limit();
        for (int i = bb.position(); i < limit; i++) {
            int c = bb.get(i) & 0xff;
            sb.append(Character.forDigit(c >> 4, 16));
            sb.append(Character.forDigit(c & 0xf, 16));
            if (i < limit - 1)
                sb.append(' ');
        }
        return sb;
    }
}

Related

  1. toHex(ByteBuffer buffer)
  2. toHex(ByteBuffer data)
  3. toHexStr(final ByteBuffer data)
  4. toHexStream(ByteBuffer data)
  5. toHexString(ByteBuffer bb)
  6. toHexString(ByteBuffer bb, boolean withSpaces)
  7. toHexString(ByteBuffer buffer)
  8. toHexString(ByteBuffer buffer)
  9. toHexString(ByteBuffer buffer, int size)