Java Binary to Hex bin2hex(byte[] arr)

Here you can find the source of bin2hex(byte[] arr)

Description

Converts a byte array to hex string

License

Apache License

Parameter

Parameter Description
arr byte array input data

Return

hex string

Declaration

public static String bin2hex(byte[] arr) 

Method Source Code

//package com.java2s;
/*/*  ww  w.j  a v a 2  s.c  om*/
Copyright 2016 SignWise Corporation Ltd.
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 {
    /**
     * Converts a byte array to hex string
     * @param arr byte array input data
     * @return hex string
     */
    public static String bin2hex(byte[] arr) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < arr.length; i++) {
            String str = Integer.toHexString((int) arr[i]);
            if (str.length() == 2)
                sb.append(str);
            if (str.length() < 2) {
                sb.append("0");
                sb.append(str);
            }
            if (str.length() > 2)
                sb.append(str.substring(str.length() - 2));
        }
        return sb.toString();
    }
}

Related

  1. bin2Hex(byte[] b)
  2. bin2Hex(byte[] bin)
  3. bin2hex(final byte[] b)
  4. bin2hex(final byte[] base)