开发者

How to control bit data in Java

I want to write to a file or a string in the finest control which is supposed to by bit-by-bit control.

For example, '00101111' (2Fx) is expected to be written to a String. Which class should I u开发者_Python百科se here? Must I use byte to present bits?


You can use the BitSet class to manipulate bits.


The answer depends on whether you want to store bit set in a human-readable format. If yes - then you can use methods like toString(int radix) with radix=2 of standard Java type classes (Integer, Long, BigInteger, ...). As a result, you will have string, each char of which represents a single bit ("11100101100").

If you want to store your bits set as effective as possible, probably you would better represent your binary data as set of chars and build string from these chars, or store binary data in BitSet class and then serialize it.


There is a method called Integer.toBinaryString() , string.format available in java 6

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜