[Fix] Fixed binaryTo*() due to not parsing in reverse

This commit is contained in:
Robert von Burg 2019-09-19 21:39:54 +02:00
parent 48142e517c
commit 936ce8ca8b
1 changed files with 16 additions and 3 deletions

View File

@ -206,13 +206,27 @@ public class StringHelper {
return b;
}
public static char binaryToChar(String binary) {
if (binary.indexOf(' ') != -1)
binary = binary.replaceAll(" ", "");
if (binary.length() != Byte.SIZE && binary.length() != Short.SIZE)
throw new IllegalArgumentException("char as binary must be 8 or 16 chars long!");
int b = 0;
for (int i = 0; i < binary.length(); i++) {
if (binary.charAt((binary.length() - 1) - i) == '1')
b = setBit(b, i);
}
return (char) b;
}
public static short binaryToShort(String binary) {
binary = binary.replaceAll(" ", "");
if (binary.length() != Short.SIZE)
throw new IllegalArgumentException("Short as binary must be 8 chars long!");
short b = 0;
for (int i = 0; i < Short.SIZE; i++) {
if (binary.charAt(i) == '1')
if (binary.charAt((Short.SIZE - 1) - i) == '1')
b = setBit(b, i);
}
@ -225,7 +239,7 @@ public class StringHelper {
throw new IllegalArgumentException("Integer as binary must be 8 chars long!");
int b = 0;
for (int i = 0; i < Integer.SIZE; i++) {
if (binary.charAt(i) == '1')
if (binary.charAt((Integer.SIZE - 1) - i) == '1')
b = setBit(b, i);
}
@ -869,5 +883,4 @@ public class StringHelper {
return uniqueId;
}
}