Java Byte to Hex Char byteToHexChars(byte value)

Here you can find the source of byteToHexChars(byte value)

Description

Converts byte value to sequence of hexadecimal characters.

License

Apache License

Parameter

Parameter Description
value byte value

Return

sequence of two hexadecimal chars with upper case

Declaration

public static char[] byteToHexChars(byte value) 

Method Source Code

//package com.java2s;
/*/*from  w ww  .  ja  v a2s . c  om*/
 * Copyright (C) ExBin Project
 *
 * 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 final char[] UPPER_HEX_CODES = "0123456789ABCDEF"
            .toCharArray();

    /**
     * Converts byte value to sequence of hexadecimal characters.
     *
     * @param value byte value
     * @return sequence of two hexadecimal chars with upper case
     */
    public static char[] byteToHexChars(byte value) {
        char[] result = new char[2];
        byteToHexChars(result, value);
        return result;
    }

    /**
     * Converts byte value to sequence of two hexadecimal characters.
     *
     * @param target target char array
     * @param value byte value
     */
    public static void byteToHexChars(char[] target, byte value) {
        target[0] = UPPER_HEX_CODES[(value >> 4) & 15];
        target[1] = UPPER_HEX_CODES[value & 15];
    }
}

Related

  1. byteToHexChar(byte b)
  2. byteToHexChars(int n)