Java Hex Calculate toHex(byte[] inBytes)

Here you can find the source of toHex(byte[] inBytes)

Description

toHex (byte []) returns the passed byte array in hex

License

Open Source License

Declaration

public static String toHex(byte[] inBytes) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//  ww w  .j a  v  a  2s  .  co  m
     * toHex (String) returns the passed string in hex
     */
    public static String toHex(String inString) {
        StringBuffer outString = new StringBuffer(4 * inString.length());
        for (int i = 0; i < inString.length(); i++) {
            String hexChar = Integer.toHexString(inString.charAt(i));
            // Make the number of hex chars even
            if (hexChar.length() % 2 == 1)
                outString.append("0");
            outString.append(hexChar);
        }
        return outString.toString();
    }

    /**
     * toHex (byte []) returns the passed byte array in hex
     */
    public static String toHex(byte[] inBytes) {
        StringBuffer outString = new StringBuffer(4 * inBytes.length);
        for (int i = 0; i < inBytes.length; i++) {
            String hexChar;
            if (inBytes[i] < 0)
                hexChar = Integer.toHexString(256 + inBytes[i]);
            else
                hexChar = Integer.toHexString(inBytes[i]);
            // Make the number of hex chars even
            if (hexChar.length() % 2 == 1)
                outString.append("0");
            outString.append(hexChar);
        }
        return outString.toString();
    }
}

Related

  1. toHex(byte[] data, int off, int len)
  2. toHex(byte[] data, int perLine, boolean offset)
  3. toHex(byte[] dBytes)
  4. toHex(byte[] digest)
  5. toHex(byte[] digest)
  6. toHex(byte[] input)
  7. toHex(byte[] key)
  8. toHex(byte[] raw)
  9. toHex(byte[] raw)