Hey,
Iv been working a lot with binary files recently and i am currently stuck trying to save a string to a file.
The problem is i do not want the literal string value of
"010100000000000100010011000000000001000100110" to be saved to the file
I want the above binary string to be saved as if it were part of the file and not just a series of 1's and 0's as this results in the fie being made of hex values 30 and 31.

OutputStream out = new FileOutputStream("MyFile");

            try {
                //add in file size at the end and append to here
                RECT b = new RECT(550,400);
                out.write(b.GETRECTANGLE().getBytes());
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            //seperate class below
    String GETRECTANGLE()
    {
    //just used for example
    String output = "010100000000000100010011000000000001000100110";
    return output;
    }

The file specification im working with states that the 45 binary bits are stored as a "bit stream" and output to a file. and thats the result i am trying to achieve, not just merely output the string to a file. Any help would be great, and the basic info for the format im working with is :
http://csharksgames.blogspot.co.uk/2010/08/hacking-swf-slides.html

Are you trying to convert the String: "010100000000000100010011000000000001000100110"
into bytes with those 0s & 1s as bits? If the String's length is not a multiple of 8, how should it be padded to fill an even number of bytes?

Can you post what you want the byte values (in hex) to be for the String you posted?

Have you tried using the Integer class's parse method with a base of 2 for each 8 characters to get a byte?

Edited 4 Years Ago by NormR1

i tried using parse, but it threw an number format exception but thats probably because i didnt do it for each 8 bits, but will the output remain the same if i split the 45 bits into 8 bit bytes and parse them? as the format specification stated that they are meant to be a bit stream with no seperation. But i will try in the mean time, thanks!

You will have to pad the String to a multiple of 8 to get exact bytes.

Comments
gave me informative and correct informaion, thanks!
This question has already been answered. Start a new discussion instead.