Java String Substritute substituteHex(String aString)

Here you can find the source of substituteHex(String aString)

Description

Substitutes hex values in aString and convert them to operating system char equivalents in the return string.

License

Open Source License

Parameter

Parameter Description
aString the string on which to apply the substitution.

Return

the string with the substitution applied.

Declaration

public static String substituteHex(String aString) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static final String HEX_OPEN = "$[";
    public static final String HEX_CLOSE = "]";

    /**//  w  w  w.  java 2s  .  c  om
     * Substitutes hex values in <code>aString</code> and convert them to
     * operating system char equivalents in the return string. Format is $[01]
     * or $[6F,FF,00,1F] Example:"This is a hex encoded six digits number 123456 in this string: $[31,32,33,34,35,36]"
     * 
     * @param aString
     *            the string on which to apply the substitution.
     * @return the string with the substitution applied.
     */
    public static String substituteHex(String aString) {
        if (aString == null)
            return null;

        StringBuffer buffer = new StringBuffer();

        String rest = aString;

        // search for opening string
        int i = rest.indexOf(HEX_OPEN);
        while (i > -1) {
            int j = rest.indexOf(HEX_CLOSE, i + HEX_OPEN.length());
            // search for closing string
            if (j > -1) {
                buffer.append(rest.substring(0, i));
                String hexString = rest.substring(i + HEX_OPEN.length(), j);
                String[] hexStringArray = hexString.split(",");
                int hexInt;
                byte[] hexByte = new byte[1];
                for (int pos = 0; pos < hexStringArray.length; pos++) {
                    try {
                        hexInt = Integer.parseInt(hexStringArray[pos], 16);
                    } catch (NumberFormatException e) {
                        hexInt = 0; // in case we get an invalid hex value,
                        // ignore: we can not log here
                    }
                    hexByte[0] = (byte) hexInt;
                    buffer.append(new String(hexByte));
                }
                rest = rest.substring(j + HEX_CLOSE.length());
            } else {
                // no closing tag found; end the search
                buffer.append(rest);
                rest = "";
            }
            // keep searching
            i = rest.indexOf(HEX_OPEN);
        }
        buffer.append(rest);
        return buffer.toString();
    }
}

Related

  1. substitute(String txt, String pattern, String sub)
  2. substituteAll(String regexp, String subst, String target)
  3. substituteBlanks(String s, String subst)
  4. substituteCommandLine(String[] parsedCommandLine, String file1Name, String file2Name, String display1, String display2)
  5. substituteEnvironmentVariables(String str, String prefix, String suffix)
  6. substituteInvalidChars(String str)
  7. substituteJobID(String path)
  8. substituteMarkers(String sql, String marker, Object substitution)
  9. substituteNull(String value, String substitution)