How to retrieve binary data from string

Reply

Join Date: Sep 2008
Posts: 34
Reputation: srs_grp is an unknown quantity at this point 
Solved Threads: 0
srs_grp srs_grp is offline Offline
Light Poster

How to retrieve binary data from string

 
0
  #1
Dec 21st, 2008
Suppose i have a string which contains some binary data as follows:

"bac01010111pqr14 "

how can i retrieve binary data from this string using java?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: How to retrieve binary data from string

 
0
  #2
Dec 21st, 2008
Just to get the terminology right, a string is just a sequence of unicode characters, there is nothing `binary' in it. In the end, everything is stored as a byte, the way the byte sequence is interpreted is what makes all the difference.

If your intention here is to grab the underlying byte representation of the string, use:
  1. String s = "bac01010111pqr14 ";
  2.  
  3. // Encodes this String into a sequence of bytes
  4. // using the platform's default charset
  5. byte[] bytes = s.getBytes();
  6.  
  7. // Encodes this String into a sequence of bytes
  8. // using the given charset
  9. byte[] bytes = s.getBytes(charSetName);
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 34
Reputation: srs_grp is an unknown quantity at this point 
Solved Threads: 0
srs_grp srs_grp is offline Offline
Light Poster

Re: How to retrieve binary data from string

 
0
  #3
Dec 21st, 2008
Originally Posted by ~s.o.s~ View Post
Just to get the terminology right, a string is just a sequence of unicode characters, there is nothing `binary' in it. In the end, everything is stored as a byte, the way the byte sequence is interpreted is what makes all the difference.

If your intention here is to grab the underlying byte representation of the string, use:
  1. String s = "bac01010111pqr14 ";
  2.  
  3. // Encodes this String into a sequence of bytes
  4. // using the platform's default charset
  5. byte[] bytes = s.getBytes();
  6.  
  7. // Encodes this String into a sequence of bytes
  8. // using the given charset
  9. byte[] bytes = s.getBytes(charSetName);

i have stored binary representation of some value in above string.
In above example i want to retrieve specifically "01010111".
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: How to retrieve binary data from string

 
0
  #4
Dec 21st, 2008
Ah, so you want to extract the sequence of digits 0 and 1 from the given string. It can be done easily by using a simple regular expression ([01])+ where the () are capturing/memory parentheses for recording or capturing the match.

I will leave the formation of an equivalent representation of the regular expression in Java to you. Look into the Pattern and Matcher class; the javadocs have everything you need to get started. Make an attempt and post the compilable code if you have any problems.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 53
Reputation: neilcoffey will become famous soon enough neilcoffey will become famous soon enough 
Solved Threads: 6
neilcoffey neilcoffey is offline Offline
Junior Poster in Training

Re: How to retrieve binary data from string

 
0
  #5
Dec 22nd, 2008
Not sure if this is what you mean, but once you have the binary number part "0101010" etc as a string (i.e. you've stripped off the other parts of the string), to convert to an int or long, look at Integer.parseInt() and Long.parseLong() -- pass in "2" as the "radix" (binary = base 2).

  1. String str = "11010101001";
  2. int number = Integer.parseInt(str, 2);
Last edited by neilcoffey; Dec 22nd, 2008 at 10:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1
Reputation: kedarp is an unknown quantity at this point 
Solved Threads: 0
kedarp kedarp is offline Offline
Newbie Poster

Re: How to retrieve binary data from string

 
0
  #6
Dec 24th, 2008
Hi,

Pl.try to solve your problem with following code.
  1. import java.util.regex.*;
  2. class Regex {
  3. public static void main(String [] args) {
  4. Pattern p = Pattern.compile("([01])");
  5. Matcher m = p.matcher("ab10001110hj14");
  6. boolean b = false;
  7. System.out.println("Pattern is " + m.pattern());
  8. while(b = m.find()) {
  9. System.out.println(m.start() + " " + m.group());
  10. }
  11. }
  12. }

I think, it will help you.

Kedar P
Last edited by ~s.o.s~; Dec 24th, 2008 at 5:17 am. Reason: Added code tags, learn to use them.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,560
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: How to retrieve binary data from string

 
0
  #7
Dec 24th, 2008
You didn't mention what you want to do with the data once you retrieve it. But you can retrieve any of the digits using theString.charAt(index). You can also use Integer.parseInt(String) once you get that digit, to see if its a 0, a 1, or neither. There might be a performance gain over that with what S.o.S said; if so, I don't know what it is.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC