Java Hex Calculate toHex(byte b)

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

Description

to Hex

License

BSD License

Declaration

public static String toHex(byte b) 

Method Source Code

//package com.java2s;
//The contents of this file are subject to the "Simplified BSD License" (the "License");

public class Main {
    public static String toHex(byte b) {
        String hex = Integer.toString((int) b & 0xFF, 16);
        if (hex.length() < 2)
            hex = "0" + hex;
        return hex;
    }//from w w  w  . j  a  v a 2 s.c  o m

    public static String toHex(byte[] data) {
        StringBuffer sb = new StringBuffer(data.length * 2);
        for (byte b : data)
            toHexByte(b, sb);
        return sb.toString();

    }

    public static void toHexByte(byte b, StringBuffer sb) {
        int n1 = (b & 0xF0) >> 4;
        int n2 = (b & 0xF);
        sb.append((char) (n1 < 10 ? n1 + '0' : (n1 - 10) + 'A'));
        sb.append((char) (n2 < 10 ? n2 + '0' : (n2 - 10) + 'A'));

    }
}

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)
  9. tohex(byte b)