Java Hex Calculate toHexString(byte[] data)

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

Description

to Hex String

License

Apache License

Declaration

public static String toHexString(byte[] data) 

Method Source Code

//package com.java2s;
/*//from  ww w.  j a v a 2s  . c  o  m
 * Copyright 2012 Sony Computer Science Laboratories, Inc. <info@kadecot.net>
 *
 * 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 static String toHexString(byte[] data) {
        return toHexString(data, " ");
    }

    public static String toHexString(byte[] data, String sep) {
        if (data == null)
            return "";
        StringBuilder sb = new StringBuilder();
        for (byte b : data) {
            sb.append(String.format("%02x" + sep, b));
        }

        return new String(sb);
    }

    public static String toHexString(byte b) {
        return String.format("%02x", b);
    }

    public static String toHexString(short s) {
        return String.format("%04x", s);
    }
}

Related

  1. toHexString(byte[] color)
  2. toHexString(byte[] content, int len)
  3. toHexString(byte[] data)
  4. toHexString(byte[] data)
  5. toHexString(byte[] data)
  6. toHexString(byte[] data)
  7. toHexString(byte[] data)
  8. toHexString(byte[] data)
  9. toHexString(byte[] data)