Java Hex Calculate toHex(byte[] key)

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

Description

to Hex

License

Apache License

Declaration

public static String toHex(byte[] key) 

Method Source Code

//package com.java2s;
/*//from  w  w  w  .ja  v a  2s . c o  m
 *  Copyright 2011-2015 Cojen.org
 *
 *  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 toHex(byte[] key) {
        return key == null ? "null" : toHex(key, 0, key.length);
    }

    public static String toHex(byte[] key, int offset, int length) {
        if (key == null) {
            return "null";
        }
        char[] chars = new char[length << 1];
        int end = offset + length;
        for (int bi = offset, ci = 0; bi < end; bi++) {
            int b = key[bi] & 0xff;
            chars[ci++] = toHexChar(b >> 4);
            chars[ci++] = toHexChar(b & 0xf);
        }
        return new String(chars);
    }

    private static char toHexChar(int b) {
        return (char) ((b < 10) ? ('0' + b) : ('a' + b - 10));
    }
}

Related

  1. toHex(byte[] dBytes)
  2. toHex(byte[] digest)
  3. toHex(byte[] digest)
  4. toHex(byte[] inBytes)
  5. toHex(byte[] input)
  6. toHex(byte[] raw)
  7. toHex(byte[] raw)
  8. toHex(byte[] raw)
  9. ToHex(byte[] src)