Java Hex Calculate toHex(int n)

Here you can find the source of toHex(int n)

Description

to Hex

License

Apache License

Declaration

public static String toHex(int n) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static String toHex(int n) {
        StringBuilder sb = new StringBuilder();
        if (n / 16 == 0) {
            return toHexUtil(n);
        } else {/*from  w w  w .j  a  va  2s  .  co m*/
            String t = toHex(n / 16);
            int nn = n % 16;
            sb.append(t).append(toHexUtil(nn));
        }
        return sb.toString();
    }

    private static String toHexUtil(int n) {
        String rt = "";
        switch (n) {
        case 10:
            rt += "A";
            break;
        case 11:
            rt += "B";
            break;
        case 12:
            rt += "C";
            break;
        case 13:
            rt += "D";
            break;
        case 14:
            rt += "E";
            break;
        case 15:
            rt += "F";
            break;
        default:
            rt += n;
        }
        return rt;
    }
}

Related

  1. toHex(int i)
  2. toHex(int i)
  3. toHex(int i, String $default)
  4. toHex(int myByte)
  5. toHex(int n)
  6. toHex(int n, Boolean bigEndian)
  7. toHex(int n, int bytes)
  8. toHex(int n, int s)
  9. toHex(int nibble)