Permalink
Browse files

Added modhex utility class

- Supports converting hex -> modhex, modhex -> hex and modhex -> Long
  • Loading branch information...
1 parent f7f7b59 commit 89e114197c9e29d261b997f864c58156e968b14d @Ratler committed Oct 9, 2011
View
@@ -80,6 +80,11 @@
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
+ <dependency>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ <version>2.6</version>
+ </dependency>
</dependencies>
<build>
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2011 United ID. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @author Stefan Wold <stefan.wold@unitedid.org>
+ */
+
+package org.unitedid.yhsm.utility;
+
+import org.apache.commons.lang.StringUtils;
+
+public class ModHex {
+ private static final String[] HEX = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
+ private static final String[] MODHEX = {"c", "b", "d", "e", "f", "g", "h", "i", "j", "k", "l", "n", "r", "t", "u", "v"};
+
+ /**
+ * Encode a hex string as modHex
+ *
+ * @param input a hex string
+ * @return modHex string
+ */
+ public static String encode(String input) {
+ return StringUtils.replaceEach(input, HEX, MODHEX);
+ }
+
+ /**
+ * Decode modHex as hex
+ *
+ * @param input a modHex string
+ * @return hex string
+ */
+ public static String decode(String input) {
+ return StringUtils.replaceEach(input, MODHEX, HEX);
+ }
+
+ /**
+ * Decode a modHex string as a decimal number
+ *
+ * @param input a modHex string
+ * @return long number
+ */
+ public static Long toDecimal(String input) {
+ return Long.decode("#" + decode(input));
+ }
+}
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2011 United ID. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @author Stefan Wold <stefan.wold@unitedid.org>
+ */
+
+package org.unitedid.yhsm.utility;
+
+import org.junit.Test;
+
+import static junit.framework.Assert.assertEquals;
+
+public class ModHexTest {
+ private final String hex = "cc55115abb";
+ private final String modHex = "rrggbbglnn";
+ private final Long decimal = 877600529083L;
+
+ @Test
+ public void encodeHex() {
+ assertEquals(modHex, ModHex.encode(hex));
+ }
+
+ @Test
+ public void decodeHex() {
+ assertEquals(hex, ModHex.decode(modHex));
+ }
+
+ @Test
+ public void modHexToDecimal() {
+ assertEquals(decimal, ModHex.toDecimal(modHex));
+ }
+}
+
@@ -22,7 +22,7 @@
import org.junit.runners.Suite;
@RunWith(Suite.class)
-@Suite.SuiteClasses(UtilsTest.class)
+@Suite.SuiteClasses({UtilsTest.class, ModHexTest.class})
public class UtilityTestSuite {
}

0 comments on commit 89e1141

Please sign in to comment.