Java Byte to Hex byteToHex(byte c)

Here you can find the source of byteToHex(byte c)

Description

Returns the hexadecimal string representation of a by

License

Open Source License

Parameter

Parameter Description
c byte to transform

Return

representation of this byte. Like : AB or 3C

Declaration

public static String byteToHex(byte c) 

Method Source Code

//package com.java2s;
/*// w w w  .j  a  va  2  s.com
Copyright (c) 2011 cockpit4, Kevin Kr?ger
    
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */

public class Main {
    /**Returns the hexadecimal string representation of a by
     * @param c byte to transform
     * @return representation of this byte. Like : AB or 3C
     */
    public static String byteToHex(byte c) {
        int l = (c & 0x0f);
        int h = (c & 0xf0) >> 4;
        char H;
        char L;

        H = h < 0x0A ? (char) (0x30 + h) : (char) (0x41 + (h - 0x0A));
        L = l < 0x0A ? (char) (0x30 + l) : (char) (0x41 + (l - 0x0A));
        return "" + H + L;
    }
}

Related

  1. byteToHex(byte b)
  2. byteToHex(byte b)
  3. byteToHex(byte b, StringBuffer buf)
  4. byteToHex(byte bytevalue)
  5. ByteToHex(byte byyte)
  6. byteToHex(byte[] bytes)
  7. byteToHex(byte[] data)
  8. byteToHex(char val)
  9. byteToHex(final byte b)