473,799 Members | 3,212 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1686
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.nos pam>
wrote in message news:ut******** ******@TK2MSFTN GP05.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.nos pam>
wrote in message news:Oc******** ******@TK2MSFTN GP04.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.bla h> 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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.bla h> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.co m>
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

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

Similar topics

2
49362
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 Java VM is different from the OpenCms encoding! Java VM file encoding: UTF-8
8
2817
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 object, it is encoded in utf-8 automatically. It can be a problem when I'm building an interactive environment using "compile", with a different encoding from utf-8. IDLE itself has the same problem. ( '<a string with non-ascii-encoding>' is...
7
4976
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 windows-1252 and that it is not equivalent to iso-8859-1. Looking in the registry under HKEY_CLASSES_ROOT\MIME\Database\Charset and \Codepage, it seems that all variations on iso-8859-1 (latin1, etc) are mapped to code page 1252, which I'm...
8
2496
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 I'm identifying the encoding type. Here is the problem, some type of UTF-8 encoded file does'nt have BOM in the starting. So I'm identying the file as iso-8859-1 encoded which is actually encoded in UTF-8. I dont have much idea about the...
8
607
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 resulting xml ends up with the following declaration: <?xml version="1.0" encoding="utf-16"?> changing the encoding to utf-16, is there something I'm doing wrong? Is it
4
8852
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 version="1.0" encoding="utf-8" ?>... - ... I am using the WebRequest functionality to get the feeds. So, my code
5
3513
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 (where "MyWebSvcName" is the name of my web service). I don't know how to assign value to this parameter. (I'm using VB.NET 2003.) Here are some sample codes:
0
3635
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 value (which varies depending on the data being written), characters not available in the encoding specified in the Encoding property of the XmlWritterSettings instance used to create the XmlWriter are being written to the resulting XML document as...
3
2492
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 typed dataset tableadaptor). In that case, I have a problem: what encoding do I use to conver the byte (or memorystream) into a string? The encoding is in the file, of course, like any XML file, but how do I find out which? I tried using an...
23
5031
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 from another HTML page viewed in IE7, that is also UTF-8 encoded (search for "china" on google.com). I paste the chineese chars into a content editable div. My Ajax webservice compiles an XML where the data from the content editable div is...
0
9687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10231
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9073
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.