Java Hex Calculate toHexString(byte b)

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

Description

Convert a byte value into a 2-digits hexadecimal value.

License

Apache License

Parameter

Parameter Description
b the byte value to convert.

Return

a string containing the 2-digit hexadecimal representation of the byte value.

Declaration

public static String toHexString(byte b) 

Method Source Code

//package com.java2s;
/*/* www  .j  a  v  a 2s .c om*/
 * JPPF.
 * Copyright (C) 2005-2010 JPPF Team.
 * http://www.jppf.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 {
    /**
     * An array of char containing the hex digits in ascending order.
     */
    private static char[] hexDigits = new char[] { '0', '1', '2', '3', '4',
            '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    /**
     * Convert a byte value into a 2-digits hexadecimal value. The first digit is 0 if the value is less than 16.<br>
     * If a value is negative, its 2-complement value is converted, otherwise the value itself is converted.
     * @param b the byte value to convert.
     * @return a string containing the 2-digit hexadecimal representation of the byte value.
     */
    public static String toHexString(byte b) {
        int n = (b < 0) ? b + 256 : b;
        StringBuilder sb = new StringBuilder();
        sb.append(hexDigits[n / 16]);
        sb.append(hexDigits[n % 16]);
        return sb.toString();
    }
}

Related

  1. toHexString(byte abyte0[], boolean spaceFlag)
  2. toHexString(byte b)
  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)