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.

Community
  • 1
  • 1
JHowIX
  • 1,683
  • 1
  • 20
  • 38
  • 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. – Shrike May 30 '13 at 22:47
  • `new String(b, Charset.forName("cp1252"))` ? – assylias May 30 '13 at 23:01
  • What characters do you expect [-98, -99, -100, -102, -104] to turn into? – Patashu May 30 '13 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]. – JHowIX May 31 '13 at 02: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? – Patashu May 31 '13 at 02:58
  • @ Shrike as a little background the individual bytes are raw bitmap values. Each byte is an 8 bit pixel. So I believe the answer to your question is that I need to interpret the stream of bytes as a stream of bytes. I don't actually want to "view" them, which is typically what a string is for. I just need to store them in a Java string so I can pass them to this function (which wont take a byte[]). – JHowIX May 31 '13 at 03:03
  • @Patashu printing is not my goal. As further background, I have a native C++ function that takes a char* as a parameter. This pointer points to the start of a memory address that the function is supposed to operate over. I am calling this native C++ function from Java using JNA. JNA's equilvaent of a char* is a Java String. I am much more inexperienced with Java than I am with C but all I need to do is convince Java that the memory stored in the byte[] is a String so it wont throw a compile error – JHowIX May 31 '13 at 03:06
  • @JHowlX In C++, doesn't a char* end at the first null byte, and data from a bitmap can have many null bytes? – Patashu May 31 '13 at 03:09
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30947/discussion-between-jhowix-and-patashu) – JHowIX May 31 '13 at 03:11

2 Answers2

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.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
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);
Richard Wilkes
  • 309
  • 2
  • 10