Java Hex Calculate toHex(byte b)

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

Description

Renders a given by to nice human readable hex format.

License

Open Source License

Parameter

Parameter Description
b The byte to be converted to a string.

Return

The result as a string, in the format 0x00.

Declaration

public static String toHex(byte b) 

Method Source Code

//package com.java2s;
/*//from  ww w .  j  a  v a  2  s . c  o m
* Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:  Helper class containing common non-HTI-specific helper
*                functions that can be used in other java classes.
*
*/

public class Main {
    /**
     * Renders a given by to nice human readable hex format.
     *
     * @param b     The byte to be converted to a string.
     * @return      The result as  a string, in the format 0x00.
     */
    public static String toHex(byte b) {
        StringBuffer sb = new StringBuffer(4);
        sb.append("0x");
        int val = b & 0xff;
        if (val < 0x10)
            sb.append("0");
        sb.append(Integer.toString(val, 16));
        return sb.toString();
    }
}

Related

  1. toHex(byte b)
  2. toHex(byte b)
  3. toHex(byte b)
  4. toHex(byte b)
  5. toHex(byte b)
  6. toHex(byte b)
  7. toHex(byte b)
  8. toHex(byte b, char[] charArray, int from)
  9. toHex(byte b, String prefix)