mirror of
https://github.com/shekhargulati/strman-java.git
synced 2026-11-18 22:04:26 +00:00
Added support for hexDecode method.
This commit is contained in:
@@ -329,11 +329,7 @@ public abstract class Strman {
|
|||||||
* @return The decoded String
|
* @return The decoded String
|
||||||
*/
|
*/
|
||||||
public static String binDecode(final String value) {
|
public static String binDecode(final String value) {
|
||||||
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
|
return decodeStringToFormat(value, 16, 2);
|
||||||
return Arrays
|
|
||||||
.stream(value.split("(?<=\\G.{16})"))
|
|
||||||
.map(data -> String.valueOf(Character.toChars(Integer.parseInt(data, 2))))
|
|
||||||
.collect(joining());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -354,11 +350,7 @@ public abstract class Strman {
|
|||||||
* @return decoded String
|
* @return decoded String
|
||||||
*/
|
*/
|
||||||
public static String decDecode(final String value) {
|
public static String decDecode(final String value) {
|
||||||
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
|
return decodeStringToFormat(value, 5, 10);
|
||||||
return Arrays
|
|
||||||
.stream(value.split("(?<=\\G.{5})"))
|
|
||||||
.map(data -> String.valueOf(Character.toChars(Integer.parseInt(data))))
|
|
||||||
.collect(joining());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -440,6 +432,25 @@ public abstract class Strman {
|
|||||||
return result;
|
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) {
|
public static String leftPad(final String value, final String pad, final int length) {
|
||||||
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
|
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
|
||||||
if (value.length() >= length) {
|
if (value.length() >= length) {
|
||||||
|
|||||||
@@ -342,4 +342,12 @@ public class StrmanTest {
|
|||||||
public void format_shouldThrowExceptionWhenValueDoesNotExist() throws Exception {
|
public void format_shouldThrowExceptionWhenValueDoesNotExist() throws Exception {
|
||||||
assertThat(format("{1} {0}"), equalTo("{1} {0}"));
|
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"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user