-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryToTextASCII.java
31 lines (24 loc) · 1.01 KB
/
BinaryToTextASCII.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.company.BinaryToTextASCII;
import com.company.KataDescription;
import java.util.Arrays;
import java.util.stream.Collectors;
@KataDescription(
name = "Binary to Text (ASCII) Conversion",
Sensei = "deanvn",
kyu = 6,
link = "https://www.codewars.com/kata/5583d268479559400d000064")
public class BinaryToTextASCII {
public static String binaryToText(String binary) {
if (binary.isEmpty()) return "";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < binary.length() / 8; i++) sb.append(binary, i * 8, i * 8 + 8).append(" ");
String res = Arrays.stream(sb.toString().split(" ")).map(value -> {
Integer res2 = 0;
for (int i = value.length() - 1; i >= 0; i--) {
res2 = res2 + (int) Math.pow(2, i) * Integer.parseInt(String.valueOf(value.charAt(Math.abs(i - 7))));
}
return String.valueOf((char) res2.intValue());
}).collect(Collectors.joining());
return res;
}
}