Java Byte Array to Hex byteToHex(byte[] content, int nLength)

Here you can find the source of byteToHex(byte[] content, int nLength)

Description

byte To Hex

License

Open Source License

Declaration

public static String byteToHex(byte[] content, int nLength) 

Method Source Code

//package com.java2s;
/****************************************************************************
 This file is part of TIImageTool.//from  w w  w  . j ava 2  s . c o  m

 TIImageTool 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 3 of the License, or
 (at your option) any later version.

 TIImageTool 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 TIImageTool.  If not, see <http://www.gnu.org/licenses/>.
    
 Copyright 2011 Michael Zapf
 www.mizapf.de
    
 ****************************************************************************/

public class Main {
    public static String byteToHex(byte[] content, int nLength) {
        StringBuilder sb = new StringBuilder();
        int nPos = 0;
        while (nPos < nLength) {
            int nByteLength = sb.length();
            sb.append(toHex(content[nPos], 2)).append(" ");
            nPos++;
        }
        return sb.toString();
    }

    public static String toHex(int value, int length) {
        return toHex(value, length, false);
    }

    public static String toHex(int value, int length, boolean bUppercase) {
        String HEX = "0123456789abcdef0123456789ABCDEF";
        char[] out = new char[length];
        int offset = bUppercase ? 16 : 0;

        for (int i = 0; i < length; i++) {
            out[length - i - 1] = HEX.charAt(offset + (value & 0x0f));
            value >>= 4;
        }
        return new String(out);
    }
}

Related

  1. byteToHex(byte[] b, int size)
  2. byteToHex(byte[] base)
  3. byteToHex(byte[] buf)
  4. byteToHex(byte[] buffer)
  5. ByteToHex(byte[] bytes)
  6. byteToHex(byte[] raw)
  7. byteToHex(final byte b)
  8. byteToHex(final byte b)
  9. bytetoHex(final byte data, final StringBuffer buffer)