Java ByteBuffer Dump toHexDumpString(ByteBuffer data)

Here you can find the source of toHexDumpString(ByteBuffer data)

Description

Generates a string hex dump from a ByteBuffer

License

Open Source License

Parameter

Parameter Description
data The source

Return

The StringBuilder with hex dump

Declaration

static public StringBuilder toHexDumpString(ByteBuffer data) 

Method Source Code

//package com.java2s;
/**//from ww w.j  av  a  2  s .co  m
 * Copyright (c) 2010-2015, openHAB.org and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

import java.nio.ByteBuffer;

public class Main {
    /**
     * Generates a hex string representative of byte data
     * @param data The source
     * @return The hex string
     */
    static public String toHexDumpString(byte data) {
        return String.format("%02X", (0xFF & data));
    }

    /**
     * Generates a string hex dump from a byte array
     * @param data The source
     * @return The StringBuilder with hex dump
     */
    static public StringBuilder toHexDumpString(byte[] data) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < data.length; i++) {
            byte c = data[i];
            if (i > 0)
                sb.append(' ');
            sb.append(toHexDumpString(c));
        }
        return sb;
    }

    /**
     * Generates a string hex dump from a ByteBuffer
     * @param data The source
     * @return The StringBuilder with hex dump
     */
    static public StringBuilder toHexDumpString(ByteBuffer data) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < data.position(); i++) {
            byte c = data.get(i);
            if (i > 0)
                sb.append(' ');
            sb.append(toHexDumpString(c));
        }
        return sb;
    }
}

Related

  1. dumpToFile(ByteBuffer buf, String fileName)
  2. hexDump(ByteBuffer buffer)
  3. hexDump(PrintStream ps, ByteBuffer bb)
  4. printByteDump(StringBuilder strBuilder, ByteBuffer data, int offset, int length, int columnNum)
  5. setDecreasedBuffer(ByteBuffer memoryDumpReader, long baseAddressValue, int innerPointerOffset, long memoryDumpStartingOffset)