5

I have a question about converting String type binary number to bit and write in the txt file.

For example we have String like "0101011" and want to convert to bit type "0101011" then write in to the file on the disk.

I would like to know is there anyway to covert to string to bit..

i was searching on the web they suggest to use bitarray but i am not sure

thanks

2
  • 3
    Do you want to write the text "0101011" to the file, or do you want to write the byte 0101011? Commented Dec 4, 2012 at 0:40
  • If we just write "010101" to file, i am guessing that it is string type file so it takes more space than bits. Therefore, in the java file we get string type binary number "010101" then convert to the bit type then i would like to write into file.. Commented Dec 4, 2012 at 0:42

3 Answers 3

4

Try this:

int value = Integer.parseInt("0101011", 2); // parse base 2

Then the bit pattern in value will correspond to the binary interpretation of the string "0101011". You can then write value out to a file as a byte (assuming the string is no more than 8 binary digits).

EDIT You could also use Byte.parseByte("0101011", 2);. However, byte values in Java are always signed. If you tried to parse an 8-bit value with the 8th bit set (like "10010110", which is 150 decimal), you would get a NumberFormatException because values above +127 do not fit in a byte. If you don't need to handle bit patterns greater than "01111111", then Byte.parseByte works just as well as Integer.parseInt.

Recall, though, that to write a byte to a file, you use OutputStream.write(int), which takes an int (not byte) value—even though it only writes one byte. Might as well go with an int value to start with.

Sign up to request clarification or add additional context in comments.

1 Comment

@A.R.S. - That would work as well, except you could not parse byte values with the 8th bit set. Besides, writing a byte to an OutputStream takes an int argument (even though it's only going to write a byte). I usually just go with an int to start with, although there's certainly a "self-documenting" aspect to using Byte.parseByte().
0

You can try the below code to avoid overflows of the numbers.

    long avoidOverflows = Long.parseLong("11000000000000000000000000000000", 2);
    int thisShouldBeANegativeNumber = (int) avoidOverflows;
    System.out.println("Currect value : " + avoidOverflows + " -> " + "Int value : " + thisShouldBeANegativeNumber);

you can see the output

Currect value : 3221225472 -> Int value : -1073741824

3 Comments

I would like to covert string type binary number "010101" to the bit type
@DcRedwing What do you mean 'bit type'? Do you mean byte?
My above answer will be useful for converting large number into proper decimal format without any overflow.
0
//Converting String to Bytes

bytes[] cipherText= new String("0101011").getBytes()

//Converting bytes to Bits and Convert to String

 StringBuilder sb = new StringBuilder(cipherText.length * Byte.SIZE);
      for( int i = 0; i < Byte.SIZE * cipherText .length; i++ )
          sb.append((cipherText [i / Byte.SIZE] << i % Byte.SIZE & 0x80) == 0 ? '0' : '1');

//Byte code of input in Stirn form

      System.out.println("Bytecode="+sb.toString());   // some binary data

 //Convert Byte To characters 

      String bin = sb.toString();
      StringBuilder b = new StringBuilder();
      int len = bin.length();
      int i = 0;
      while (i + 8 <= len) {
        char c = convert(bin.substring(i, i+8));
        i+=8;
        b.append(c);
      }

//String format of Binary data

      System.out.println(b.toString());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.