Added support for hexDecode method.

This commit is contained in:
Shekhar Gulati
2016-05-20 18:37:38 +05:30
parent b42ff8338e
commit 34ea17ffd0
2 changed files with 29 additions and 10 deletions
+21 -10
View File
@@ -329,11 +329,7 @@ public abstract class Strman {
* @return The decoded String
*/
public static String binDecode(final String value) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return Arrays
.stream(value.split("(?<=\\G.{16})"))
.map(data -> String.valueOf(Character.toChars(Integer.parseInt(data, 2))))
.collect(joining());
return decodeStringToFormat(value, 16, 2);
}
/**
@@ -354,11 +350,7 @@ public abstract class Strman {
* @return decoded String
*/
public static String decDecode(final String value) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return Arrays
.stream(value.split("(?<=\\G.{5})"))
.map(data -> String.valueOf(Character.toChars(Integer.parseInt(data))))
.collect(joining());
return decodeStringToFormat(value, 5, 10);
}
/**
@@ -440,6 +432,25 @@ public abstract class Strman {
return result;
}
/**
* Convert hexadecimal unicode (4 digits) string to string chars
*
* @param value The value to decode
* @return The decoded String
*/
public static String hexDecode(final String value) {
return decodeStringToFormat(value, 4, 16);
}
private static String decodeStringToFormat(final String value, int digits, int radix) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return Arrays
.stream(value.split("(?<=\\G.{" + digits + "})"))
.map(data -> String.valueOf(Character.toChars(Integer.parseInt(data, radix))))
.collect(joining());
}
public static String leftPad(final String value, final String pad, final int length) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
if (value.length() >= length) {
+8
View File
@@ -342,4 +342,12 @@ public class StrmanTest {
public void format_shouldThrowExceptionWhenValueDoesNotExist() throws Exception {
assertThat(format("{1} {0}"), equalTo("{1} {0}"));
}
@Test
public void hexDecode_shouldDecodeHexCodeToString() throws Exception {
assertThat(hexDecode("6f22"), equalTo("漢"));
assertThat(hexDecode("0041"), equalTo("A"));
assertThat(hexDecode("00c1"), equalTo("Á"));
assertThat(hexDecode("00410041"), equalTo("AA"));
}
}