How to Get 0-Padded Binary Representation of an Integer in Java

How to get 0-padded binary representation of an integer in java?

I think this is a suboptimal solution, but you could do

String.format("%16s", Integer.toBinaryString(1)).replace(' ', '0')

How to get 0-padded binary representation of an integer in java?

I think this is a suboptimal solution, but you could do

String.format("%16s", Integer.toBinaryString(1)).replace(' ', '0')

Pad a binary String equal to zero ( 0 ) with leading zeros in Java

For padding with, say, 5 leading zeroes, this will work:

String.format("%5s", Integer.toBinaryString(data)).replace(' ', '0');

You didn't specify the expected length of the string, in the sample code above I used 5, replace it with the proper value.

EDIT

I just noticed the comments. Sure you can build the pattern dynamically, but at some point you have to know the maximum expected size, depending on your problem, you'll know how to determine the value:

String formatPattern = "%" + maximumExpectedSize + "s";

Adding zeroes to front and end of an integer - binary output

As you say, you were almost done. But the binary encoding wasn't working as expected. Here is my suggestion:

  public static void getBinaryMsg(String codedMessage) {
String binary = toBinary(codedMessage);
System.out.println(binary);
}

private static String toBinary(String codedMessage) {
String binary = codedMessage.chars().boxed().map(c -> pad(Integer.toBinaryString(c), 8, '0') + " ").collect(Collectors.joining());
return binary;
}

private static String pad(String s, int n, char c) {
return String.format("%"+n+"s", Integer.parseInt(s)).replace(' ', c);
}

Using Integer.toBinaryString(int i) you don't have to reinvent the wheel. The only thing you have to add is the padding, to get every binary formatted to eight bits. You did it well according to: How to get 0-padded binary representation of an integer in java?

Java Printing Leading 0's in Binary

Use String format like on this example:

byte b2 = (byte) 2;
String s2 = String.format("%8s", Integer.toBinaryString(b2 & 0xFF)).replace(' ', '0');
System.out.println(s2); // 00000010

Integer.toBinaryString() Loses Leading 0's

I assume that you're looking to have enough leading 0s to make the length of the String that is returned from Integer#toBinaryString 8; the following code will achieve this for you:

String binary = String.format("%8s", Integer.toBinaryString(currentByte)).replace(' ', '0');

How to get 0-padded binary representation of an integer in R language

The R.utils package has the function intToBin() that can be combined with sprintf() to left pad the result.

convert_to_b <- function(n, K)
{
n <- R.utils::intToBin(n)
if (K < nchar(n))
K <- nchar(n)
sprintf("%0*d", K, as.integer(n))
}

convert_to_b(3, 7)
[1] "0000011"

Converting a decimal number to binary in java not showing leading zeroes

I assume you are looking to format all your answers as shorts (16 bits).

In this case, simply check the length of your current string, and add on zeroes as needed.

int zeroesRemaining = 16 - result.length();
for (int i = 0; i < zeroesRemaining; i++) {
result = "0" + result;
}

Alternatively, if you want to do it faster, use a StringBuilder.

int zeroesRemaining = 16 - result.length();
StringBuilder tempBuilder = new StringBuilder(result);
for (int i = 0; i < zeroesRemaining; i++) {
tempBuilder.insert(0, 0); //inserts the integer 0 at position 0 of the stringbuilder
}
return tempBuilder.toString(); //converts to string format

There is also probably a formatter that could do this, but I don't know of such.

If you want to change the number of zeroes to be the closest integer primitive, just set zeroesRemaining to be the (least power of 2 that is greater than the number of bits) minus (the number of bits).

One 0 is missing in my binary - how can I fix it?

Instead of binary = Integer.toBinaryString(character);

use the following expression:

binary = String.format("%8s", Integer.toBinaryString(character)).replace(' ', '0');


Related Topics



Leave a reply



Submit