• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting byte to BitSet

 
Christiaan Lombard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I want to write a program that can read the header properties of a .mp3 file. If you don't know what I'm talking about, visit: MPEG AUDIO FRAME HEADER

The problem is that FileInputStream only allows you to read byte by byte. I need to convert that byte into something like a BitSet so I can read each bit in the byte.

The solution I came up with was:
1. Read the byte
2. Convert it to String using Integer.toBinaryString(byte)
3. Pad the String with zeros so there is 8 bits that can be read from it.

I have not tested this part since I'm still hoping there is a better way to do it (and it probably won't work).

Here is what I've done so far to retrieve the header and tag parts of the file:



Thanks
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure that this is a lot faster:

 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could look at the bits the old fashioned way using Java's bitwise operators.

Bill
 
Christiaan Lombard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:I'm pretty sure that this is a lot faster:



Yes I would think so too. I guess it's time to start studying bitwise operators. It sucks that they took it out of SCJP 6.

Thanks
 
Stephen Longville
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

any ideas how to convert back?

thanks.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Loop over the length of the BitSet, take each bit value, and bitwise or it: value |= bit << index. Note that BitSet is not as limited as int or long so you can get larger results out of them. The int / long will then be truncated.
 
Stephen Longville
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
like this?



still not familiar whith how | / & works with bit opperations
also not quite sure what was meant by value and bit.

thanks.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why use a one-element byte array? What's wrong with just a byte?

That code is almost what I meant. You use b[0] for the value. The thing is, you don't test if the bit is set or not. Use the result of bits.get(i). If this returns false you don't need to do anything.
 
Henry Wong
author
Marshal
Pie
Posts: 21008
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Brogden wrote:Or you could look at the bits the old fashioned way using Java's bitwise operators.


Agreed++. Why bother converting to a bit set, when it is just as easy to check a bit, set a bit, and clear a bit with the bitwise operators. Heck, with the bitwise operators, you can check, set, or clear multiple bits simultaneously.

Henry
 
Stephen Longville
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so this then;




the reason i am using an array instead of a byte is beacuse i am then using System.arraycopy() to copy each byte into a larger byte array for writing
(its part of a WAVE file steganography project)
does it work just the same as if it were a non array single byte? (im assuming it would because its still a byte)

thanks.
 
Stephen Longville
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
William Brogden wrote:Or you could look at the bits the old fashioned way using Java's bitwise operators.


Agreed++. Why bother converting to a bit set, when it is just as easy to check a bit, set a bit, and clear a bit with the bitwise operators. Heck, with the bitwise operators, you can check, set, or clear multiple bits simultaneously.

Henry


by this do you mean for example to read bits from an input stream rarther than bytes?
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephen Longville wrote:the reason i am using an array instead of a byte is beacuse i am then using System.arraycopy() to copy each byte into a larger byte array for writing
(its part of a WAVE file steganography project)
does it work just the same as if it were a non array single byte? (im assuming it would because its still a byte)

Sure. Instead of using System.arraycopy(b, 0, destination, offset, b.length) (with b being a byte[]) you use destination[offset] = b (with b being a byte).
 
Stephen Longville
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hehe there i go over complicating things again thanks for clearing that up Rob

now im sure my byte to bit (and vice versa) functions are correct my problem still exists, if you feel like taking a look its HERE on the ranch.

thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic