| | |
How to retrieve binary data from string
![]() |
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:
If your intention here is to grab the underlying byte representation of the string, use:
Java Syntax (Toggle Plain Text)
String s = "bac01010111pqr14 "; // Encodes this String into a sequence of bytes // using the platform's default charset byte[] bytes = s.getBytes(); // Encodes this String into a sequence of bytes // using the given charset byte[] bytes = s.getBytes(charSetName);
I don't accept change; I don't deserve to live.
•
•
Join Date: Sep 2008
Posts: 34
Reputation:
Solved Threads: 0
•
•
•
•
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:Java Syntax (Toggle Plain Text)
String s = "bac01010111pqr14 "; // Encodes this String into a sequence of bytes // using the platform's default charset byte[] bytes = s.getBytes(); // Encodes this String into a sequence of bytes // using the given charset 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".
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
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.
([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.
•
•
Join Date: Dec 2008
Posts: 53
Reputation:
Solved Threads: 6
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).
Java Syntax (Toggle Plain Text)
String str = "11010101001"; int number = Integer.parseInt(str, 2);
Last edited by neilcoffey; Dec 22nd, 2008 at 10:49 pm.
•
•
Join Date: Dec 2008
Posts: 1
Reputation:
Solved Threads: 0
Hi,
Pl.try to solve your problem with following code.
I think, it will help you.
Kedar P
Pl.try to solve your problem with following code.
Java Syntax (Toggle Plain Text)
import java.util.regex.*; class Regex { public static void main(String [] args) { Pattern p = Pattern.compile("([01])"); Matcher m = p.matcher("ab10001110hj14"); boolean b = false; System.out.println("Pattern is " + m.pattern()); while(b = m.find()) { System.out.println(m.start() + " " + m.group()); } } }
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.
•
•
Join Date: Sep 2008
Posts: 1,560
Reputation:
Solved Threads: 196
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.
![]() |
Similar Threads
- Repair Access database (VB.NET)
- How to configure GD (PHP)
- php.ini confusion (PHP)
- Help with reading data from a file (C++)
- Send data on a serial port (C++)
- Binary Search Tree (Java)
- Login and retrieve user data from database (ASP.NET)
- To display an icon in a datagrid (VB.NET)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
- not-a-virusadware (Viruses, Spyware and other Nasties)
Other Threads in the Java Forum
- Previous Thread: Looking for open source message queue implementation
- Next Thread: java question!
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows






