Encoding Problem?

  • Thread starter Thread starter Andrew
  • Start date Start date
A

Andrew

I'm trying to pass a byte array from C# to Java.

I used a BinaryReader in C# to read the byte array.

I'm using a DataInputStream in Java to read the byte array.

The data is passing through just fine, and I'm getting the correct
number of bytes, but the data doesn't look right. :(

I assume this is an encoding issue.

How do I create a byte array in C# that is compatible with Java?

What encoding should I use?

Thanks,

Andrew
 
Do you mean what kind of encoding should you use to get your byte array
that you're going to pass to JAVA? If you already have a byte array,
and you just want to access it from a JAVA function there should be no
"encoding" necessary.
 
Andrew said:
I'm trying to pass a byte array from C# to Java.

I used a BinaryReader in C# to read the byte array.

I'm using a DataInputStream in Java to read the byte array.

The data is passing through just fine, and I'm getting the correct
number of bytes, but the data doesn't look right. :(

I assume this is an encoding issue.

Why do you assume that?
How do I create a byte array in C# that is compatible with Java?

A byte array is a byte array.
What encoding should I use?

What are you putting into the byte array? How are you concluding that it's
wrong?

-cd
 
Carl Daniel said:
Why do you assume that?

Because I don't know what else it could be. :)
A byte array is a byte array.

Agreed. Bad question.

What are you putting into the byte array? How are you concluding that
it's wrong?

I'm reading in an MP3 file.

I created two programs. One in C# and one in Java.

I read in the bytes and printed each byte (as an int) to the console.

I compared the bytes and noticed that some of the bytes were different.

Andrew
 
Andrew said:
"Carl Daniel [VC++ MVP]"
What are you putting into the byte array? How are you concluding
that it's wrong?

I'm reading in an MP3 file.

I created two programs. One in C# and one in Java.

I read in the bytes and printed each byte (as an int) to the console.

I compared the bytes and noticed that some of the bytes were
different.

Why/how are you using a BinaryReader? To simply read a byte stream from a
file, you should simply use the Read function on a Stream object (most
likely, a FileStream, since you say you're reading a file). You don't need
to use any kind of "reader" to read uninterpreted bytes from a stream.

-cd
 
Why/how are you using a BinaryReader? To simply read a byte stream from a
file, you should simply use the Read function on a Stream object (most
likely, a FileStream, since you say you're reading a file). You don't
need to use any kind of "reader" to read uninterpreted bytes from a
stream.

OK. I just ditched the BinaryReader.

Not sure why I was using it in the first place, it just seemed like an easy
solution. :)

I'm now using the Read method from FileStream.

However, it still doesn't work right and it looks like the bytes are
different.

Here is the very end of the same file. For example, in C# the byte is 170,
in Java it is -86

Reading bytes using C#:

255 250 56 192 221 50 230 128 20 237 31 105 185
156 0 17 88 14 222 135 178 240 0 170 170 170 170 170 170 170 170 170 170 170
1
70 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170
170 1
70 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170
170 1
70 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170
170 1
70 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170
170 1
70 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170
170 1
70 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170
170 1
70 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170
170 1
70 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170
170 1
70 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170
170 1
70 170 84 65 71 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32
32 3
2 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32
32
32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32
32 32
32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32
32 3
2 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 0

Reading bytes in Java:

-1 -6 56 -64 -35 50 -26 -128 20 -19 31 105 -71 -100 0 17 88
14 -34 -121 -78 -16
0 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86
-86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86
-86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86
-86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86
-86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86
-86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86
-86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86
-86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86
-86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86
-86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86 -86
-86 -86 -86 84 65 71 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32
32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32
32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32
32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32
32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32
32 32 32 32 32 32 0
 
Andrew said:
Here is the very end of the same file. For example, in C# the byte is 170,
in Java it is -86

<snip>

That's just because in C# bytes are unsigned, and in Java they're
signed.
 
Jon Skeet said:
That's just because in C# bytes are unsigned, and in Java they're
signed.

I take it that means I need to convert my unsigned byte array to a signed
array?

Is there a method or class that will do this for me?

Andrew
 
Andrew said:
I take it that means I need to convert my unsigned byte array to a signed
array?

Not really.
Is there a method or class that will do this for me?

What are you trying to do with the data after you've read it? That's
the important thing.
 
Jon Skeet said:
What are you trying to do with the data after you've read it? That's
the important thing.

Well, let's say I wanted to send the data to a Java server and use
a DataInputStream to read the byte array.

Would this require any conversion to handle the unsigned byte array
that I acquired in C Sharp?

Andrew
 
Andrew said:
Well, let's say I wanted to send the data to a Java server and use
a DataInputStream to read the byte array.

Would this require any conversion to handle the unsigned byte array
that I acquired in C Sharp?

Nope, no conversion required.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top