1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.vcollaborate.bitwise;
21
22 import com.google.common.base.Joiner;
23 import com.google.common.base.Splitter;
24 import com.google.common.base.Strings;
25
26 import java.util.List;
27
28 public final class BinaryStringUtils {
29 public static final char SEPARATOR = '_';
30
31 private BinaryStringUtils() {
32 }
33
34
35
36
37 public static final String prettyPrint(final String binaryString) {
38 return prettyPrint(binaryString, SEPARATOR);
39 }
40
41
42
43
44 public static final String prettyPrint(final String binaryString, final char separator) {
45 String paddedBinaryString = zeroPadString(binaryString);
46 List<String> splitted = Splitter.fixedLength(4).splitToList(paddedBinaryString);
47 return Joiner.on(separator).join(splitted);
48 }
49
50
51
52
53 public static final String fromPrettyString(final String prettyBinaryString) {
54 return fromPrettyString(prettyBinaryString, SEPARATOR);
55 }
56
57
58
59
60 public static final String fromPrettyString(final String prettyBinaryString,
61 final char separator) {
62 List<String> splitted = Splitter.on(separator).splitToList(prettyBinaryString);
63 return Joiner.on("").join(splitted);
64 }
65
66
67
68
69 public static final String zeroPadString(final String binaryString, final int minLength) {
70 return Strings.padStart(binaryString, minLength, '0');
71 }
72
73
74
75
76 public static final String zeroPadString(final String binaryString) {
77 int minLength = (int) Math.ceil(binaryString.length() / 4.) * 4;
78 return zeroPadString(binaryString, minLength);
79 }
80
81 }