Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions utilcode/src/main/java/com/blankj/utilcode/util/EncodeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,34 @@ public static CharSequence htmlDecode(final String input) {
return Html.fromHtml(input);
}
}

/**
* Return the binary encoded string padded with one space
*
* @param input
* @return binary string
*/
public static String binEncode(final String input) {
StringBuilder stringBuilder = new StringBuilder();
for (char i : input.toCharArray()) {
stringBuilder.append(Integer.toBinaryString(i));
stringBuilder.append(' ');
}
return stringBuilder.toString();
}

/**
* Return UTF-8 String from binary
*
* @param input binary string
* @return UTF-8 String
*/
public static String binDecode(final String input){
String[] splitted = input.split(" ");
StringBuilder sb = new StringBuilder();
for(String i : splitted){
sb.append(((char) Integer.parseInt(i.replace(" ", ""), 2)));
}
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,10 @@ public void htmlEncode_htmlDecode() {

assertEquals(html, EncodeUtils.htmlDecode(encodeHtml).toString());
}
@Test
public void binEncode_binDecode(){
String test = "test";
String binary = EncodeUtils.binEncode(test);
assertEquals("test", EncodeUtils.binDecode(binary));
}
}