Java Hex Calculate toHexString(byte[] coded)

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

Description

to Hex String

License

Apache License

Declaration

public static String toHexString(byte[] coded) 

Method Source Code

//package com.java2s;
/**//from  w  w w. j av a2  s. com
 * 
 *Copyright 2014 The Darks Codec Project (Liu lihua)
 *
 *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 {
    private static final char[] HEX_DIGIT = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
            'C', 'D', 'E', 'F' };

    public static String toHexString(byte[] coded) {
        if (coded == null) {
            return "";
        }
        return toHexString(coded, 0, coded.length);
    }

    public static String toHexString(byte[] coded, int offset, int length) {
        if (coded == null) {
            return "";
        }
        StringBuilder result = new StringBuilder(length * 3);
        for (int i = 0; i < length; i++) {
            int c = coded[i + offset];
            if (c < 0) {
                c += 256;
            }
            int hex1 = c & 0xF;
            int hex2 = c >> 4;
            result.append(HEX_DIGIT[hex2]);
            result.append(HEX_DIGIT[hex1]);
            result.append(' ');
        }
        return result.toString();
    }
}

Related

  1. toHexString(byte[] bytes)
  2. toHexString(byte[] bytes)
  3. toHexString(byte[] bytes, boolean addPrefix)
  4. toHexString(byte[] bytes, int offset, int len, int max)
  5. toHexString(byte[] bytes, String separator, boolean upperCase)
  6. toHexString(byte[] color)
  7. toHexString(byte[] content, int len)
  8. toHexString(byte[] data)
  9. toHexString(byte[] data)