Index: src/org/openstreetmap/josm/tools/Utils.java
===================================================================
--- src/org/openstreetmap/josm/tools/Utils.java	(revision 6091)
+++ src/org/openstreetmap/josm/tools/Utils.java	(working copy)
@@ -401,6 +401,8 @@
         return toHexString(byteDigest);
     }
 
+    private static final char[] HEX_ARRAY = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
+
     /**
      * Converts a byte array to a string of hexadecimal characters.
      * Preserves leading zeros, so the size of the output string is always twice
@@ -409,13 +411,22 @@
      * @return hexadecimal representation
      */
     public static String toHexString(byte[] bytes) {
-        char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
-        char[] hexChars = new char[bytes.length * 2];
-        for (int j=0; j<bytes.length; j++) {
-            int v = bytes[j] & 0xFF;
-            hexChars[j*2] = hexArray[v/16];
-            hexChars[j*2 + 1] = hexArray[v%16];
+
+        if (bytes == null){
+            return "";
         }
+
+        final int len = bytes.length;
+        if (len == 0){
+            return "";
+        }
+
+        char[] hexChars = new char[len * 2];
+        for (int i = 0, j = 0; i < len; i++) {
+            final int v = bytes[i];
+            hexChars[j++] = HEX_ARRAY[(v & 0xf0) >> 4];
+            hexChars[j++] = HEX_ARRAY[v & 0xf];
+        }
         return new String(hexChars);
     }
 
Index: test/unit/org/openstreetmap/josm/tools/UtilsTest.java
===================================================================
--- test/unit/org/openstreetmap/josm/tools/UtilsTest.java	(revision 6091)
+++ test/unit/org/openstreetmap/josm/tools/UtilsTest.java	(working copy)
@@ -50,4 +50,18 @@
         Assert.assertEquals("ab", Utils.strip(someWhite+"ab"+someWhite));
         Assert.assertEquals("abc", Utils.strip(someWhite+"abc"+someWhite));
     }
+
+    /**
+     * Test of {@link Utils#toHexString} method.
+     */
+    @Test
+    public void testToHexString(){
+        Assert.assertEquals("", Utils.toHexString(null));
+        Assert.assertEquals("", Utils.toHexString(new byte[0]));
+        Assert.assertEquals("01", Utils.toHexString(new byte[]{0x1}));
+        Assert.assertEquals("0102", Utils.toHexString(new byte[]{0x1,0x2}));
+        Assert.assertEquals("12", Utils.toHexString(new byte[]{0x12}));
+        Assert.assertEquals("127f", Utils.toHexString(new byte[]{0x12, 0x7f}));
+        Assert.assertEquals("fedc", Utils.toHexString(new byte[]{(byte) 0xfe, (byte) 0xdc}));
+    }
 }
