Java Byte Array to Hex bytes2HexStr(byte[] bytes)

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

Description

bytes Hex Str

License

Apache License

Declaration

public final static String bytes2HexStr(byte[] bytes) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w .j ava 2s  .  c  o  m*/
 * Copyright Bruce Liang (ldcsaa@gmail.com)
 *
 * Version   : JessMA 3.5.1
 * Author   : Bruce Liang
 * Website   : http://www.jessma.org
 * Project   : http://www.oschina.net/p/portal-basic
 * Blog      : http://www.cnblogs.com/ldcsaa
 * WeiBo   : http://weibo.com/u/1402935851
 * QQ Group   : 75375912
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {

    public final static String bytes2HexStr(byte[] bytes) {
        return bytes2HexStr(bytes, false);
    }

    public final static String bytes2HexStr(byte[] bytes, boolean capital) {
        StringBuilder sb = new StringBuilder();

        for (byte b : bytes)
            sb.append(byte2Hex(b, capital));

        return sb.toString();
    }

    public final static char[] byte2Hex(byte b) {
        return byte2Hex(b, false);
    }

    public final static char[] byte2Hex(byte b, boolean capital) {
        byte bh = (byte) (b >>> 4 & 0xF);
        byte bl = (byte) (b & 0xF);

        return new char[] { halfByte2Hex(bh, capital), halfByte2Hex(bl, capital) };
    }

    public final static char halfByte2Hex(byte b) {
        return halfByte2Hex(b, false);
    }

    public final static char halfByte2Hex(byte b, boolean capital) {
        return (char) (b <= 9 ? b + '0' : (capital ? b + 'A' - 0xA : b + 'a' - 0xA));
    }
}

Related

  1. bytes2Hex(byte[] bytes)
  2. bytes2HexCharArr(byte[] bytes)
  3. bytes2HexPP2(byte[] bytes)
  4. bytes2HexSplit(byte[] in, int wordlength)
  5. bytes2HexStr(byte[] bytes)
  6. bytes2HexString(byte... bytes)
  7. bytes2HexString(byte[] array)
  8. bytes2HexString(byte[] b)
  9. bytes2HexString(byte[] b)