Java Hex Calculate toHex(String in, boolean javaStyle)

Here you can find the source of toHex(String in, boolean javaStyle)

Description

to Hex

License

Open Source License

Declaration

public static String toHex(String in, boolean javaStyle) 

Method Source Code

//package com.java2s;

public class Main {
    public static String toHex(String in, boolean javaStyle) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < in.length(); ++i) {
            result.append(toHex(in.charAt(i), javaStyle));
        }//from   ww  w  .ja v  a 2 s. com
        return result.toString();
    }

    public static String toHex(int j, boolean javaStyle) {
        if (j == '\"') {
            return "\\\"";
        } else if (j == '\\') {
            return "\\\\";
        } else if (0x20 < j && j < 0x7F) {
            return String.valueOf((char) j);
        }
        final String hexString = Integer.toHexString(j).toUpperCase();
        int gap = 4 - hexString.length();
        if (gap < 0) {
            gap = 0;
        }
        String prefix = javaStyle ? "\\u" : "U+";
        return prefix + "000".substring(0, gap) + hexString;
    }
}

Related

  1. toHex(long i, int r0, boolean r1)
  2. toHex(long l, int length)
  3. toHex(Long value, int charCount)
  4. toHex(long value, int length)
  5. toHex(String color)
  6. toHEX(String ostr)
  7. toHex(String source)
  8. toHex(String str)
  9. toHex(String string)