Java Hex Calculate toHexDec(byte[] bytes)

Here you can find the source of toHexDec(byte[] bytes)

Description

Format first 200 bytes into hex, followed by decimal presentation.

License

Open Source License

Parameter

Parameter Description
bytes bytes to format

Return

formatted bytes as string

Declaration

public static String toHexDec(byte[] bytes) 

Method Source Code

//package com.java2s;
/*//from   www.  j ava  2  s  .com
Calimero - A library for KNX network access
Copyright (C) 2006-2008 W. Kastner
    
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; either version 2 of the License, or
(at your option) any later version.
    
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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

public class Main {
    /**
     * Format first 200 bytes into hex, followed by decimal presentation.
     * <p>
     * 
     * @param bytes bytes to format
     * @return formatted bytes as string
     */
    public static String toHexDec(byte[] bytes) {
        final StringBuffer buf = new StringBuffer();
        final int max = Math.min(200, bytes.length);
        for (int i = 0; i < max; ++i) {
            final String hex = Integer.toHexString(bytes[i] & 0xff);
            if (hex.length() == 1)
                buf.append("0");
            buf.append(hex);
            buf.append(" ");
        }
        if (max < bytes.length)
            buf.append("...");
        buf.append("(");
        for (int i = 0; i < max; ++i) {
            final String no = Integer.toString(bytes[i] & 0xff);
            buf.append(no);
            if (i < bytes.length - 1)
                buf.append(" ");
        }
        if (max < bytes.length)
            buf.append("...");
        buf.append(")");
        return buf.toString();
    }
}

Related

  1. toHexCharArray(byte[] input)
  2. toHexCharFromBin(final String bin)
  3. toHexChars(byte[] bs)
  4. toHexChars(byte[] bytes)
  5. toHexChars(final int b)
  6. toHexDigit(char ch)
  7. toHexDigit(char ch, int index)
  8. toHexDigit(int d)
  9. toHexDigit(int d)