(Java) What is the number of bits (length) when converting a binary number to a string?

I am trying to store a number as a binary string in an array, but I need to specify how many bits will store it as.

For example, if I need to store 0 with two bits, I need the string "00". Or 1010 with 6 bits so "001010".

Can anyone help?

EDIT: Thanks guys, since I'm rubbish in math / programming in general, I went with the simplest solution that was David. Something like:

binaryString.append(Integer.toBinaryString(binaryNumber)); for(int n=binaryString.length(); n<numberOfBits; n++) { binaryString.insert(0, "0"); } 

Everything seems to be fine, so if it is not very effective, I will go with it.

+7
java binary
source share
8 answers

Use Integer.toBinaryString() , then check the length of the string and add it with as many zeros as you need to make the desired length.

+9
source share

Forget home solutions. Use the standard BigInteger instead. You can specify the number of bits, and then use the toString (int radix) method to restore what you need (I assume you need radix = 2).

EDIT: I would leave bit control for BigInteger. The object will internally resize the bit buffer to fit the new size. In addition, arithmetic operations can be performed using this object (you do not need to implement binary adders / factors, etc.). Here is an example:

 package test; import java.math.BigInteger; public class TestBigInteger { public static void main(String[] args) { String value = "1010"; BigInteger bi = new BigInteger(value,2); // Arithmetic operations System.out.println("Output: " + bi.toString(2)); bi = bi.add(bi); // 10 + 10 System.out.println("Output: " + bi.toString(2)); bi = bi.multiply(bi); // 20 * 20 System.out.println("Output: " + bi.toString(2)); /* * Padded to the next event number of bits */ System.out.println("Padded Output: " + pad(bi.toString(2), bi.bitLength() + bi.bitLength() % 2)); } static String pad(String s, int numDigits) { StringBuffer sb = new StringBuffer(s); int numZeros = numDigits - s.length(); while(numZeros-- > 0) { sb.insert(0, "0"); } return sb.toString(); } } 
+4
source share

This is a common home problem. There, a cool loop that you can write will calculate the least power 2> = your target number n.

Since it has a power of 2, the logarithm of base 2 is the number of bits. But the Java math library offers only the natural logarithm.

 math.log( n ) / math.log(2.0) 

is the number of bits.

+3
source share

Even simpler:

 String binAddr = Integer.toBinaryString(Integer.parseInt(hexAddr, 16)); String.format("%032", new BigInteger(binAddr)); 

The idea here is to print the string back as a decimal number temporarily (what happens is all 1 and 0), and then use String.format ().

Note that basically you should use BigInteger because binary strings quickly overflow Integer and Long, which results in NumberFormatExceptions if you try to use Integer.fromString() or Long.fromString() .

+1
source share

Try the following:

 String binaryString = String.format("%"+Integer.toString(size)+"s",Integer.toBinaryString(19)).replace(" ","0"); 

where the size can be any number the user wants

+1
source share
 import java.util.BitSet; public class StringifyByte { public static void main(String[] args) { byte myByte = (byte) 0x00; int length = 2; System.out.println("myByte: 0x" + String.valueOf(myByte)); System.out.println("bitString: " + stringifyByte(myByte, length)); myByte = (byte) 0x0a; length = 6; System.out.println("myByte: 0x" + String.valueOf(myByte)); System.out.println("bitString: " + stringifyByte(myByte, length)); } public static String stringifyByte(byte b, int len) { StringBuffer bitStr = new StringBuffer(len); BitSet bits = new BitSet(len); for (int i = 0; i < len; i++) { bits.set (i, (b & 1) == 1); if (bits.get(i)) bitStr.append("1"); else bitStr.append("0"); b >>= 1; } return reverseIt(bitStr.toString()); } public static String reverseIt(String source) { int i, len = source.length(); StringBuffer dest = new StringBuffer(len); for (i = (len - 1); i >= 0; i--) dest.append(source.charAt(i)); return dest.toString(); } } 

Output:

 myByte: 0x0 bitString: 00 myByte: 0x10 bitString: 001010 
0
source share

Here's a simple solution for int values; It should be obvious how to expand it to, for example, bytes, etc.

 public static String bitString(int i, int len) { len = Math.min(32, Math.max(len, 1)); char[] cs = new char[len]; for (int j = len - 1, b = 1; 0 <= j; --j, b <<= 1) { cs[j] = ((i & b) == 0) ? '0' : '1'; } return new String(cs); } 

Here is the result from a set of test cases:

  0 1 0 0 0 -1 0 0 0 40 00000000000000000000000000000000 00000000000000000000000000000000 13 1 1 1 13 2 01 01 13 3 101 101 13 4 1101 1101 13 5 01101 01101 -13 1 1 1 -13 2 11 11 -13 3 011 011 -13 4 0011 0011 -13 5 10011 10011 -13 -1 1 1 -13 40 11111111111111111111111111110011 11111111111111111111111111110011 

Of course, you yourself can make the length parameter sufficient to represent the entire value.

0
source share

So, instead of 8, you can write the desired length and add zeros accordingly. If the length of the integers you specify exceeds the specified number, then it will not add zeros

String.format("%08d",1111);

Output: 00001111

 String.format("%02d",1111); 

output: +1111

0
source share

All Articles