473,386 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Encoding Problem?

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


Jun 18 '06 #1
11 1658
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 wrote:
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


Jun 18 '06 #2
Andrew wrote:
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
Jun 18 '06 #3
> Do you mean what kind of encoding should you use to get your byte array
that you're going to pass to JAVA?


Yes, I believe that's what I'm looking for. :)

Andrew
Jun 18 '06 #4

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:ut**************@TK2MSFTNGP05.phx.gbl...
I assume this is an encoding issue.
Why do you assume that?


Because I don't know what else it could be. :)

How do I create a byte array in C# that is compatible with Java?
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
Jun 18 '06 #5
Andrew wrote:
"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
Jun 18 '06 #6

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:Oc**************@TK2MSFTNGP04.phx.gbl...
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

Jun 18 '06 #7
Andrew <no***@jose.blah> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 18 '06 #8

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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

Jun 18 '06 #9
Andrew <no***@jose.blah> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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?


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 18 '06 #10

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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

Jun 19 '06 #11
Andrew <no***@jose.blah> wrote:
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?


Nope, no conversion required.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 19 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ann | last post by:
Hi, Is there any way to Change encoding of Java Vm to ISO-8859-1? i am using Java vm along with an application called opencms. I get the following error message.. Error: the encoding of your...
8
by: janeaustine50 | last post by:
Python's InteractiveInterpreter uses the built-in compile function. According to the ref. manual, it doesn't seem to concern about the encoding of the source string. When I hand in an unicode...
7
by: Mark | last post by:
Hi... I've been doing a lot of work both creating and consuming web services, and I notice there seems to be a discontinuity between a number of the different cogs in the wheel centering around...
8
by: davisjoseph | last post by:
Hi All, I'm newbie to this XML world. My problem is to identify the encoding type of XML at runtime. What currently I'm doing is checking whether BOM is available in the XML; based on the BOM...
8
by: Demon News | last post by:
I'm trying to do a transform (Using XmlTransform class in c#) and in the Transform I'm specifying the the output xsl below: <xsl:output method="xml" encoding="UTF-8" indent="no"/> the...
4
by: fitsch | last post by:
Hi, I am trying to write a generic RSS/Atom/OPML feed client. The problem is, that those xml feeds may have different encodings: - <?xml version="1.0" encoding="ISO-8859-1" ?>... - <?xml...
5
by: James Wong | last post by:
Dear all, I've a web service function and it contains a parameter in System.Text.Encoding. I found that the data type of this parameter in caller application becomes MyWebSvcName.Encoding...
0
by: Janusz Nykiel | last post by:
I've stumbled upon unexpected behavior of the .NET 2.0 System.Xml.XmlWriter class when using it to write data to a binary stream (System.IO.Stream). If the amount of data is less than a certain...
3
by: Martin Z | last post by:
Hi, I have an application that involves sending a lot of XML data to various places. The problem is that once in a while, I just want the XML document as a string (for example, sending to a...
23
by: Allan Ebdrup | last post by:
I hava an ajax web application where i hvae problems with UTF-8 encoding oc chineese chars. My Ajax webapplication runs in a HTML page that is UTF-8 Encoded. I copy and paste some chineese chars...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.