HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMajor

Hexadecimal converter using a switch statement

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
hexadecimalstatementusingswitchconverter

Problem

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': hexadecimal = "\\3e"; break;
                case '@': hexadecimal ="\\40"; break;
                case '\\':hexadecimal = "\\5c"; break;
                default: {
                    if (Character.isWhitespace(c)) {
                        hexadecimal = "\\20";
                    }else if(c > 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.

Solution

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 hexMap = new HashMap();
hexMap.put('\"', "\\22");
hexMap.put('\n', "\\20");
// puts more stuff in map...

 for (int i=0, n=node.length(); i<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 < node.length(); ++i) {
        char ch = node.charAt(i);
        buf.append(String.format("\\%1$x", (ch & 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.

Code Snippets

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

 for (int i=0, n=node.length(); i<n; i++) {
     char c = node.charAt(i);
     buf.append(hexMap.get(c));
 }
StringBuilder buf = new StringBuilder(node.length() + 8);

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

return buf.toString();

Context

StackExchange Code Review Q#13389, answer score: 29

Revisions (0)

No revisions yet.