I needed to convert decimal numbers in a string to binary. I needed to use it for a program.
I tried this code:
static String numbers = "48, 40, 6, 82, 222, 94, 152, 46, 77";
public static void main(String[] args) {
for (int i = 0; i < 255; i++) {
numbers = numbers.replaceAll(Integer.toString(i), Integer.toBinaryString(i));
}
System.out.println(numbers);
}
What I expected in the console:
110000, 101000, 110, 1010010, 11011110, 1011110, 10011000, 101110, 1001101
What actually happened:
11011011110010110110111100100110110111100101101101111001000,
110110111100101101101111001000,
11011011110010110111101101111001010,
1101101111001011011011110010001101101111001010,
110110111100101011011011110010101101101111001010,
11011011110010110110111100100110110111100101101111011011110010110110111100100,
1101101111001011011110110111100101011011011110010110111101101111001010,
1101101111001011011011110010011011011110010110111101101111001010,
11011011110010110111110110111100101101111101101111001011
I think that the converter (for
loop) is converting numbers that are already converted (eg. 10, 110, 100
)
You're setting yourself up for failure by looping over an obscure range of numbers and doing strange string replacements. Instead, I would recommend splitting the string and processing
each number individually:
String numbers = "48, 40, 6, 82, 222, 94, 152, 46, 77";
StringJoiner joiner = new StringJoiner(", ");
for (String number : numbers.split("\\, ")) {
int i = Integer.parseInt(number);
String binary = Integer.toBinaryString(i);
joiner.add(binary);
}
System.out.println(joiner.toString());
If you want to get fancy, you can use a one-liner:
String numbers = "48, 40, 6, 82, 222, 94, 152, 46, 77";
String binary = Arrays.stream(numbers.split("\\, ")).map(Integer::parseInt).map(Integer::toBinaryString).collect(Collectors.joining(", "));
System.out.println(binary);
Or, if you're like me and you overthink performance and optimization, you can also use a scanner with a custom delimiter. This may improve runtime complexity, as the example above has a runtime complexity of at least O(2n)
. It's also worth noting how the underlying implementation for String#split
uses an ArrayList
, which may also affect performance:*
String numbers = "48, 40, 6, 82, 222, 94, 152, 46, 77";
StringJoiner joiner = new StringJoiner(", ");
@SuppressWarnings("resource")
Scanner scanner = new Scanner(numbers).useDelimiter(", ");
while (scanner.hasNext()) {
int i = Integer.parseInt(scanner.next());
joiner.add(Integer.toBinaryString(i));
}
System.out.println(joiner.toString());
* String#split
builds an array list under the hood and then calls List#toArray(String[])
. Depending on the string and regex used, this list could potentially expand several times. I'm unsure of the actual performance cost associated with expanding an array list or converting it to an array, due to that fact that it uses System.arraycopy
under the hood. See this related question: Why is System.arraycopy native in Java?