Hexadecimal converter using a switch statement I need some help improving my code and wondering if I there is a better way to do the following:

StringBuilder buf = new StringBuilder(node.length() + 8);
        for (int i=0, n=node.length(); i&lt;n; i++) {
            char c = node.charAt(i);
            String hexadecimal = "";
            switch (c) {
                case '"': hexadecimal = "\\22"; break;
                case '&amp;': hexadecimal = "\\26"; break;
                case '\'': hexadecimal = "\\27"; break;
                case '/':  hexadecimal = "\\2f"; break;
                case ':': hexadecimal = "\\3a"; break;
                case '&lt;': hexadecimal = "\\3c"; break;
                case '&gt;': hexadecimal = "\\3e"; break;
                case '@': hexadecimal ="\\40"; break;
                case '\\':hexadecimal = "\\5c"; break;
                default: {
                    if (Character.isWhitespace(c)) {
                        hexadecimal = "\\20";
                    }else if(c &gt; 127){
                        hexadecimal = "\\" + Integer.toHexString(c) + "\\";
                    }
                    else {
                        hexadecimal = String.valueOf(c);
                    }
                }
            }
            buf.append(hexadecimal);
        }
        return buf.toString();


I don't like how there is a switch statement which just sets a variable (hexadecimal) based on what the case is. I feel there is a better way to do this, such as by using a hash-map. I need the code to be flexible so that adding new characters and setting the hexadecimal is easy.
A hash map is often a good idea for avoiding switch statements. It can be used in combination with the Strategy and Abstract Factory patterns, such as in these examples.

However, the Strategy Pattern is overkill for your problem. A simple hashmap would look like this

Map&lt;Character, String&gt; hexMap = new HashMap&lt;Character, String&gt;();
hexMap.put('\"', "\\22");
hexMap.put('\n', "\\20");
// puts more stuff in map...

 for (int i=0, n=node.length(); i&lt;n; i++) {
     char c = node.charAt(i);
     buf.append(hexMap.get(c));
 }


But a quick googling of "java character to hexadecimal" produces these results. My favorite is

StringBuilder buf = new StringBuilder(node.length() + 8);

    for (int i = 0; i &lt; node.length(); ++i) {
        char ch = node.charAt(i);
        buf.append(String.format("\\%1$x", (ch &amp; 0xFFFF)));
    }

return buf.toString();


EDIT: Forgot to say, don't use magic numbers. I have no clue what the frack that 8 is doing in your program nor do I have to patience to try to deduce.