0

The question sound dumb but it's like that

I had a binary string which contains a series of "0" and "1". If I store its as textfile it'll be huge since each character take about 8 bytes. But actually it should be much lesser since "0" or "1" is just actually 1 bit (1 byte/8). My question is how could to do that in Java?

Edited to avoid confuse

I've used an algorithm to encode all the data down to "0" and "1" sequence string. So 1 chunk of data could become quite big ... a few A4 paper of 1 string contains "0" and "1".

I'm stuck at the step to write down that string (which just "0" and "1") to an actual binary data file ... and it should be much lesser than just store down the text file which contains "0" and "1" as I assumed

Van Vu
  • 193
  • 1
  • 8

3 Answers3

0

Without talking implementation, a simple encoding scheme would be to store the decimal value of your binary string. That way, your computer will simply store the binary string as the bits in a number of the appropriate size (int/short/long/etc). Dunno if that would have the most elegant read/write process, though.

dpbont
  • 91
  • 1
  • 8
0

If you store in binary, it's 2 symbols you have.

If you store it in hexadecimal, it's 16 symbols you have.

More symbols, less chars in a text file.

What most people would do, if storage is importante for you, is to store your data in a binary format, and then compressing it (with DataOutputStream and ZipOutputStream).

Zip is pretty fast ;-)

update after the downvote (don't shoot the messenger - or at least explain your downvote... help us help you)

or you can use something like this

BigInteger bigInt = new BigInteger("100010001000100010001000100010011000011100010001000100010001000100010011000011111111111111111111111111111111111111111111111100",2);

BigDecimal bigDec = new BigDecimal(bigInt);
Leo
  • 6,480
  • 4
  • 37
  • 52
  • Well I'm not the one down vote but could you have a look again as my updated question ... Convert it to an bigInt would be a ncie idea with very very small chunk of binary but how about a few a4 ... string ? – Van Vu Mar 04 '14 at 05:54
  • 1
    a4 string? well, if you have a big string, you could just split it and save several bigints, I guess. – Leo Mar 04 '14 at 12:12
0

I've done more googling and below is the solution in my case... I've put down the solution there incase that someone would have the same question as mine :)

After read convert a binary string representation to a byte array in C#. I've decided to implement its Java version

int numOfBytes = binarytring.length() / 8;
byte[] bytes = new byte[numOfBytes];

//store it down as 1 byte (8bits) each
for(int i = 0; i < numOfBytes; ++i) {
    // thanks https://stackoverflow.com/questions/6658388/why-java-throws-a-numberformatexception for help me out of the exception.
    bytes[i] = (byte) (Integer.parseInt(encoded.substring(8 * i, (8 * i) + 8), 2) & 0xFF);
}

FileOutputStream fos = new FileOutputStream("outputfilename"); 
fos.write(bytes);
fos.close();
Community
  • 1
  • 1
Van Vu
  • 193
  • 1
  • 8