Convert BitSet to Byte Array

kmerkison
kmerkison used Ask the Experts™
on
I'm doing a file compression program and I need to know how to convert a BitSet to a byte array.. I've tried using the following code, where bits is my BitSet and bytearr is defined as the byte array..

      bytearr = bits.toBytes();


Also, I need to be able to convert from a byte array into a BitSet..
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
I've also tried the toByteArray() function.. i may not be using the function properly.. not sure.. i've tried..

bytearr = bits.toByteArray();
bytearr = toByteArray(bits);
Hi,
try this code:
...
// init new bit set
  BitSet bitSet = new BitSet(10);
  bitSet.set(4);
  bitSet.set(8);
  bitSet.set(9);

  // convert from BitSet to byte array
  ByteArrayOutputStream baos = new ByteArrayOutputStream(bitSet.size());
  try {
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(bitSet);
  }
  catch (Exception ex) {
    ex.printStackTrace();
  }
  byte[] byteArr = baos.toByteArray(); // the byte array

  // convert back to BitSet
  BitSet bitSet2 = null; // the new BitSet
  ByteArrayInputStream bais = new ByteArrayInputStream(byteArr);
  try {
    ObjectInputStream ois = new ObjectInputStream(bais);
    bitSet2 = (BitSet)ois.readObject();
  }
  catch (Exception ex) {
    ex.printStackTrace();
  }

  // check :
  System.out.println(bitSet.equals(bitSet2));
...


this code is good for all objects that implements java.io.Serializable - not only for BitSet.
-gkern
the same code as two methods:

// convert from BitSet to byte array
  public static byte[] toByteArray(BitSet bitSet) {
     ByteArrayOutputStream baos = new ByteArrayOutputStream(bitSet.size());
     try {
       ObjectOutputStream oos = new ObjectOutputStream(baos);
       oos.writeObject(bitSet);
     }
     catch (Exception ex) {
       ex.printStackTrace();
     }
     return baos.toByteArray();
  }

  // convert from byte array to BitSet
  public static BitSet toBitSet(byte[] byteArr) {
     BitSet bitSet = null; // the new BitSet
     ByteArrayInputStream bais = new ByteArrayInputStream(byteArr);
     try {
       ObjectInputStream ois = new ObjectInputStream(bais);
       bitSet = (BitSet)ois.readObject();
     }
     catch (Exception ex) {
       ex.printStackTrace();
     }

     return bitSet;
  }

the usage in the code:
...
BitSet bitSet = new BitSet();
...
byte[] bytearr = toByteArray(bitSet);
...
BitSet bitSet2 = toBitSet(bytearr); // bitSet equals bitSet2
...

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

-  Points for gkern

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venabili
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial