recreating a BitSet from its toString()

A

Andreas Leitgeb

I wonder if there is a simple trick, idiom or standard
utility, to re-create a BitSet from a String that was
previously generated by a BitSet's .toString() method.

Such a String usually looks like: "{0, 1, 2, 3, 5, 8, 13, 21}"

I could trim the braces, then split it at the commata,
and then parse each item to int, and .set() it in a
fresh BitSet, but maybe I'm just missing something
easier.

PS: I do not want standard object-serialization, as it must
be a human read+understandable text-representation.
Other such textual representations are ok, too, if they
make parsing easier.
 
A

Andreas Leitgeb

Andreas Leitgeb said:
I wonder if there is a simple trick, idiom or standard
utility, to re-create a BitSet from a String ...
PS: [...]
Other such textual representations are ok, too, if they
make parsing easier.

I ended up with a string rep like "1,1,2,3,5,8,end"
Which made it easiest to get back in with a Scanner
(which I already used for enclosing parts):

while (sc.hasNextInt()) { bs.set(sc.nextInt()); } sc.next("end");

Exceptions are caught one level upstairs. It's not a webapp,
so anyone who manipulates the string rep, could just as well
do anything else with his computer...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top