File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed
main/java/com/blankj/utilcode/util
test/java/com/blankj/utilcode/util Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -183,4 +183,34 @@ public static CharSequence htmlDecode(final String input) {
183
183
return Html .fromHtml (input );
184
184
}
185
185
}
186
+
187
+ /**
188
+ * Return the binary encoded string padded with one space
189
+ *
190
+ * @param input
191
+ * @return binary string
192
+ */
193
+ public static String binEncode (final String input ) {
194
+ StringBuilder stringBuilder = new StringBuilder ();
195
+ for (char i : input .toCharArray ()) {
196
+ stringBuilder .append (Integer .toBinaryString (i ));
197
+ stringBuilder .append (' ' );
198
+ }
199
+ return stringBuilder .toString ();
200
+ }
201
+
202
+ /**
203
+ * Return UTF-8 String from binary
204
+ *
205
+ * @param input binary string
206
+ * @return UTF-8 String
207
+ */
208
+ public static String binDecode (final String input ){
209
+ String [] splitted = input .split (" " );
210
+ StringBuilder sb = new StringBuilder ();
211
+ for (String i : splitted ){
212
+ sb .append (((char ) Integer .parseInt (i .replace (" " , "" ), 2 )));
213
+ }
214
+ return sb .toString ();
215
+ }
186
216
}
Original file line number Diff line number Diff line change @@ -70,4 +70,10 @@ public void htmlEncode_htmlDecode() {
70
70
71
71
assertEquals (html , EncodeUtils .htmlDecode (encodeHtml ).toString ());
72
72
}
73
+ @ Test
74
+ public void binEncode_binDecode (){
75
+ String test = "test" ;
76
+ String binary = EncodeUtils .binEncode (test );
77
+ assertEquals ("test" , EncodeUtils .binDecode (binary ));
78
+ }
73
79
}
You can’t perform that action at this time.
0 commit comments