Java Hex Calculate toHexString(byte b)

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

Description

to Hex String

License

Mozilla Public License

Return

The value of b as hex string (e.g. 0xd3 will return "d3").

Declaration

public static String toHexString(byte b) 

Method Source Code

//package com.java2s;
//License from project: Mozilla Public License 

public class Main {
    /** @return The value of <code>b</code> as hex string
     * (e.g. <code>0xd3</code> will return <code>"d3"</code>). */
    public static String toHexString(byte b) {
        StringBuffer sb = new StringBuffer("00");
        sb.append(Integer.toHexString(b));
        return sb.substring(sb.length() - 2);
    }//from w  ww .j av  a2 s . co  m

    /** @return The value of <code>i</code> as hex string
     * (e.g. <code>0xd3</code> will return <code>"d3"</code>). */
    public static String toHexString(int i) {
        StringBuffer sb = new StringBuffer("00000000");
        sb.append(Integer.toHexString(i));
        return sb.substring(sb.length() - 8);
    }

    /** @return The value of <code>i</code> as hex string
     * (e.g. <code>0xd3</code> will return <code>"d3"</code>). */
    public static String toHexString(long l) {
        StringBuffer sb = new StringBuffer("0000000000000000");
        sb.append(Long.toHexString(l));
        return sb.substring(sb.length() - 16);
    }

    /** @return The value of <code>bytes</code> as hex string
     * with the lowest index left. 
     * <code>bytes[0] = 0xab</code> and <code>bytes[1] = 0xcd</code>  
     * will return <code>"abcd"</code>. */
    public static String toHexString(byte[] bytes) {
        StringBuffer sb = new StringBuffer(bytes.length * 2);
        for (int iH = 0; iH < bytes.length; iH++)
            sb.append(toHexString(bytes[iH]));
        return sb.toString();
    }
}

Related

  1. toHexString(byte b)
  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)