Suppose i have a string which contains some binary data as follows:

"bac01010111pqr14 "

how can i retrieve binary data from this string using java?

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:

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);

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:

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 ([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.

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).

String str = "11010101001";
int number = Integer.parseInt(str, 2);

Hi,

Pl.try to solve your problem with following code.

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

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.

This article has been dead for over six months. Start a new discussion instead.