Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to represent bitstring in java  RSS feed

 
dheeraj chhabra
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi u all...

i m developing a application java........I have to take input from user and represent that in bitstring .I have to divide the bitstring into octets. And to perform some binary operation .

For example...
" i have i have to take a input bitstring 11100101101 from the user and divide this into octets like 11100101 101xxxxx (where x represent unused bits filled with 0 or 1's whatever)."


So can anybody plz tell me how can i perform this...
ur little assistance would help me in making my application..
thanking you.
[ May 09, 2005: Message edited by: dheeraj chhabra ]
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you check out the java.util.BitSet class?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Stuart, I'd never looked at that. It's a little thin on getting things out, isn't it. Wouldn't it be nice to get an array of booleans or a string of 1s and 0s? The toString() method puts out the Java syntax we use to initialize arrays, but you'd have to parse it to figure out which bits are set. Maybe those methods are just what you'd have to build for this task.

Also see if you can use Integer parseInt with base 2 to convert your string of 1s and 0s to an int. You can do your bit manipulation on the int and convert back to string for display. Be careful ... ints are signed so you don't want to use the high order bunch of bits.
[ May 09, 2005: Message edited by: Stan James ]
 
dheeraj chhabra
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying ..........
But the BitSet class doesn't solve the purpose......

I just wanna know how to take input from user in bitstring...........?

Plz help me out of this..........

waiting for response :roll:
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the BitSet class is a bit weak; it would be nice to have a whole host of useful methods in there.

dheeraj, if you are only wanting to verify input then take a look at the JFormattedTextField class and DocumentFormat.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you detail your requirements a bit better? Maybe sketch out a test that would show what you expect.
 
dheeraj chhabra
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My requirement is this:-

I have to take input as a string..such as "111110001101" coz i cannot take as bitstring
Now i have to convert this string into bitstring.....such as each digit(0 or 1) would represent a bit......ok?

Now plz help me ............
waiting......
bbye
 
Tom Katz
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to get 8character strings from your input string? e.g: "10001000", "00010001" - if thats the case you should just loop through the input and use substring. If you want the actual numerical values of the resulting 8-bit bytes, you'll probably have to use the bit manipulation operators (>>, <<, >>> , but I havent had enough coffee to be able to figure out exactly what you're trying to do.
 
Tom Katz
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to get 8character strings from your input string? e.g: "10001000", "00010001" - if thats the case you should just loop through the input and use substring. If you want the actual numerical values of the resulting 8-bit bytes, you'll probably have to use the bit manipulation operators (>>, <<, >>> , but I havent had enough coffee to be able to figure out exactly what you're trying to do.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!