aspose file tools*
The moose likes Java in General and the fly likes Converting byte to BitSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Converting byte to BitSet" Watch "Converting byte to BitSet" New topic
Author

Converting byte to BitSet

Christiaan Lombard
Greenhorn

Joined: Nov 22, 2009
Posts: 26
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


SCJP 6
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I'm pretty sure that this is a lot faster:


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12675
    
    5
Or you could look at the bits the old fashioned way using Java's bitwise operators.

Bill

Java Resources at www.wbrogden.com
Christiaan Lombard
Greenhorn

Joined: Nov 22, 2009
Posts: 26
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

Joined: Mar 25, 2010
Posts: 15
hi

any ideas how to convert back?

thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Stephen Longville
Greenhorn

Joined: Mar 25, 2010
Posts: 15
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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
Sheriff

Joined: Sep 28, 2004
Posts: 18103
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Stephen Longville
Greenhorn

Joined: Mar 25, 2010
Posts: 15
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

Joined: Mar 25, 2010
Posts: 15
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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

Joined: Mar 25, 2010
Posts: 15
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting byte to BitSet
 
Similar Threads
baffling UTF-8 problem
FileInputStream problem
DataOutputStream / WAVE steganography
how to insert carriage return
Java nio