0

I have an array of bytes b:

b = [-98, -99]

I need to pass this segment of data to a function. The function takes a String (this is not changeable). How do I get Java to interpret the raw binary data in b as a string without changing any of the bits in b

EDIT

This is not a repeat of:

Converting byte array to String (Java)

Because, the array b consists of the bits [1001 1110 1001 1101] or [9E9D]. If I use

String str = new String(b, "Cp1252")

The Cp1252 encoding does not have a value for the seqeuence of bits 1001 1101 or 9D. So this function results in the the str = "ž?". Which in binary form is [1001 1110 0011 1111] or [9E3F]. Notice the function has changed my bits, which essentially corrupts my data.

I think this would be done in C++ using a reinterpret cast but no such thing exists in java. Thanks in advance.

9
  • 1
    What's the encoding of the bytes? You can't interpret a stream of bytes into a String until you know what the individual bytes mean. Commented May 30, 2013 at 22:47
  • new String(b, Charset.forName("cp1252")) ? Commented May 30, 2013 at 23:01
  • What characters do you expect [-98, -99, -100, -102, -104] to turn into? Commented May 30, 2013 at 23:03
  • @Patashu Their 8 bit hex equivalents. -98 is FFFF FFFF FFFF FF9E. Its stored in a byte array (8 bits per array element) so by its hex equivalent should be 9E. By similar logic [-98, -99, -100, -102, -104] should be [9E, 9D, 9C, 9A, 98]. Commented May 31, 2013 at 2:56
  • @JHowlX What I mean is, all of those characters are not representable as ASCII. So what encoding do you expect/what characters do you expect to be printed? Commented May 31, 2013 at 2:58

2 Answers 2

1

You probably want something like new String(b, "charset")); where charset is replaced by the encoding you want to use. I would suggest UTF-8, except your values aren't valid UTF-8.

Sign up to request clarification or add additional context in comments.

Comments

0

You cannot do this with arbitrary bytes of any value, as there is no encoding that will allow that, and I'm assuming you have no control over the decoding of the data. If you do have control over the decoding as well as the encoding, you could write your own Charset so that you can map the data into and back out of a normal Java string, which is encoded as UTF-16. To encode it:

String str = new String(byteBuffer, MyCharSet);

To decode it:

byte[] byteBuffer = str.getBytes(MyCharSet);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.