Java Hex Calculate toHexString(byte[] b)

Here you can find the source of toHexString(byte[] b)

Description

Format a byte array as a hexadecimal string.

License

LGPL

Declaration

public static String toHexString(byte[] b) 

Method Source Code

//package com.java2s;
/*/*  w w  w. j a  va2  s .  c o m*/
 * Copyright 2005 MBARI
 *
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 2.1 
 * (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.gnu.org/copyleft/lesser.html
 *
 * 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 {
    /**
     * Format a byte array as a hexadecimal string. For example
     * <pre>
     * byte[] bytes = new byte[]{(byte)0x12, (byte)0x0F, (byte)0xF0};
     * String hex = NumberUtilities.toHexString(bytes); // 120ff0
     *</pre>
     *
     */
    public static String toHexString(byte[] b) {
        if (b == null) {
            return "null";
        }
        StringBuffer ret = new StringBuffer(b.length);
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(0x0100 + (b[i] & 0x00FF)).substring(1);
            ret.append((hex.length() < 2 ? "0" : "") + hex);
        }
        return ret.toString();

    }
}

Related

  1. toHexString(byte[] array)
  2. toHexString(byte[] array)
  3. toHexString(byte[] b)
  4. toHexString(byte[] b)
  5. toHexString(byte[] b)
  6. toHexString(byte[] b)
  7. toHexString(byte[] b)
  8. toHexString(byte[] b)
  9. toHexString(byte[] b)