I have a string as follows:


I want to retrieve contents of the string as follows:
1st field: mnp
2nd field: 101 (which signifies the binary representation of 5)
3rd field: 1000 (which signifies the binary representation of 8)
4th field: jie

I know that binary integer requires 4 bytes of storage.String considers one byte of storage for each character. Just retrieving 4 bytes from 'mnp' onwards will give 1011 which is a mess up of two distinct binary integers stored side by side.
The main problem is that I am not able to retrieve the two consecutive binary numbers stored in a string.How can I proceed bytewise to extract the two consecutive binary numbers?

Pattern Matching classes are not proving useful in this case.If I use these classes,then I get an output as follows:
1 0 1 1 0 0 0
which is not the expected output.

The expected output is:

1st field: mnp
2nd field: 101 (which signifies the binary representation of 5)
3rd field: 1000 (which signifies the binary representation of 8)
4th field: jie

How can I do this USING CORE JAVA?

Recommended Answers

All 3 Replies

> one byte of storage for each character

The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive). Read me.

Why is it that you are required to come up with such a hack just to extract some data? Where is the data coming from? Doesn't every field have some fixed width? Maybe explaining a bit about what you are trying to achieve here rather than how you are trying to achieve it might fetch you some good answers.

And like I have previously mentioned, don't go on confusing bytes and characters; a string is just a sequence of unicode characters. So if you have a string "abc010101xyz", the "0" there is just a character, not an integer, not a binary digit. Just come up with a specification of how your data will be formatted and process the string as a sequence of unicode characters rather than bytes and bits.

commented: Very well said +5

I think I'm missing something here -- as I did when you asked a very similar thing in another thread.

If you just want to extract the substring between two character positions, then use the substring() method. So if str is "mnp1011000jie", then do:

String str1 = str.substring(0, 3); // "mnp"
String str2 = str.substring(3, 6); // "101"
String str3 = str.substring(6, 10); // "1000"

If you want to convert a string such as "101" into an actual int, then use Integer.parseInt(str2, 2); etc.

If this isn't your problem, then you need to state clearly what the problem is because nobody really understands what you're trying to do...

As sos correctly mentions it there should be a clear fomation of the data that you want to parse, only then will you be able to parse it correctly in all cases. For this you would either have to have a fixed width for each field or distinct termination characters for each of them (distinct = ones that would not appear as data).
Without such a proper format parsing anything would be a very weak technique to reach to the correct value of the actual data one that would most probably terminate in wrongly parsed data (as you show in your eg)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.