Java Hex Calculate toHex(byte[] data, int perLine, boolean offset)

Here you can find the source of toHex(byte[] data, int perLine, boolean offset)

Description

Convert a byte array to a hexidacimal representation as a string.

License

Open Source License

Parameter

Parameter Description
data bytes to be converted
perLine hex pairs to before line seperator
offset output offsets

Return

a string represination of the hexadecimal convertion of the byte array

Declaration

public static String toHex(byte[] data, int perLine, boolean offset) 

Method Source Code

//package com.java2s;
/**//from  w  w  w. jav a 2 s.  c om
 * <PRE>
 * Name   : com.solidmatrix.regxmaker.util.shared.ConsoleUtils
 * Project: RegXmaker Library
 * Version: 1.1
 * Tier   : N/A (Function Class)
 * Author : Gennadiy Shafranovich
 * Purpose: General utilities for console based applications
 *
 * Copyright (C) 2001, 2004 SolidMatrix Technologies, Inc.
 * This file is part of RegXmaker Library.
 *
 * RegXmaker Library is is free software; you can redistribute it and/or modify
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * RegXmaker library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Comments: Full, with javadoc.
 *
 * Modification History
 *
 * 01-28-2001  GS Created
 * 07-02-2001  GS Added a more general readFull() method to read 
 *                entire contents of a stream. readFullFile now
 *                used this method to read from an underlying
 *                FileInputStream.
 *
 * 07-05-2004 YS Added licensing information
 * </PRE>
 */

public class Main {
    private static String line = System.getProperty("line.separator");

    /**
     * Convert a byte array to a hexidacimal representation as a string.
      * 
     * @param   data  bytes to be converted
     * @param   perLine  hex pairs to before line seperator
     * @param   offset  output offsets
     * @return     a string represination of the hexadecimal convertion of the byte array
     */
    public static String toHex(byte[] data, int perLine, boolean offset) {

        //get size of buffer that will be needed
        int size = data.length * 3;
        int lines = size / perLine;

        if (offset)
            size += lines * 13;

        size += lines;

        StringBuffer sb = new StringBuffer(size);
        StringBuffer sb1 = new StringBuffer(13);

        for (int i = 0; i < data.length; i += perLine) {

            if (offset) {
                sb1.delete(0, sb1.length());

                String t = Integer.toHexString(i);

                while (t.length() + sb1.length() < 8)
                    sb1.append("0");

                sb1.append(t.toUpperCase());

                sb.append(sb1.toString());
                sb.append(":    ");
            } //if

            for (int j = i; j < i + perLine && j < data.length; j++) {
                String b = Integer.toHexString(data[j]).toUpperCase();

                if (b.length() == 1)
                    sb.append("0");
                else
                    b = b.substring(b.length() - 2, b.length());

                sb.append(b);
                sb.append(" ");
            } //for

            sb.append(line);
        } //for

        return sb.toString();
    }
}

Related

  1. toHex(byte[] data)
  2. toHex(byte[] data)
  3. toHex(byte[] data, int bytesPerGroup)
  4. toHex(byte[] data, int length)
  5. toHex(byte[] data, int off, int len)
  6. toHex(byte[] dBytes)
  7. toHex(byte[] digest)
  8. toHex(byte[] digest)
  9. toHex(byte[] inBytes)