Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   JNI byte array to string (http://www.velocityreviews.com/forums/t134396-jni-byte-array-to-string.html)

static 06-15-2004 04:18 PM

JNI byte array to string
 
Hi,

I use JNI to call a C function and get a record converted from MARC-8
to UNICODE and return the data in a byteArray. It works fine and the
byteArray is correctly populated. I can write it out to a file and
verified the data.

The problem is converting the byte array to a string. If I do

String n = new String(test);

Then about 6 of the characters get replaced with question marks. Is
there a way to retain all of the data from a byte array and convert it
to a String?

I also tried

String n = new String(test,"UTF-8");

and that didn't work. A few characters got replaced with question
marks.

Any ideas will be greatly appreciated.

Ashley

ak 06-15-2004 06:30 PM

Re: JNI byte array to string
 
> I use JNI to call a C function and get a record converted from MARC-8
> to UNICODE and return the data in a byteArray. It works fine and the
> byteArray is correctly populated. I can write it out to a file and
> verified the data.
>
> The problem is converting the byte array to a string. If I do
>
> String n = new String(test);
>
> Then about 6 of the characters get replaced with question marks. Is
> there a way to retain all of the data from a byte array and convert it
> to a String?
>
> I also tried
>
> String n = new String(test,"UTF-8");


don't create String, but read it with DataInputStream#readUTF();

--
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader



static 06-16-2004 07:58 PM

Re: JNI byte array to string
 
I tried the following and am still getting some characters in the byte
array changed to question marks.

try {
DataInputStream dis = new DataInputStream(new
ByteArrayInputStream(unicode_byte_array));
orig = dis.readLine();
}
catch (IOException e)
{
//System.out.println(e);
}

Since readLine is deprecated, is there another way to read the data
from the byte array and not change it. readString will change it and
since it is already in UTF-8, doing a readUTF8 corrupts the data by
translating something that is already in utf8.

Thanks in advance.

Ashley

"ak" <spam@imagero.com> wrote in message news:<canf8t$rf4$1@online.de>...
> > I use JNI to call a C function and get a record converted from MARC-8
> > to UNICODE and return the data in a byteArray. It works fine and the
> > byteArray is correctly populated. I can write it out to a file and
> > verified the data.
> >
> > The problem is converting the byte array to a string. If I do
> >
> > String n = new String(test);
> >
> > Then about 6 of the characters get replaced with question marks. Is
> > there a way to retain all of the data from a byte array and convert it
> > to a String?
> >
> > I also tried
> >
> > String n = new String(test,"UTF-8");

>
> don't create String, but read it with DataInputStream#readUTF();


Roedy Green 06-16-2004 08:56 PM

Re: JNI byte array to string
 
On 16 Jun 2004 12:58:10 -0700, static_maelstrom@yahoo.com (static)
wrote or quoted :

>
>Since readLine is deprecated, is there another way to read the data
>from the byte array and not change it. readString will change it and
>since it is already in UTF-8, doing a readUTF8 corrupts the data by
>translating something that is already in utf8.


There are many possible things you could be trying to do. You first
have to get clear on just what your data are.

1. 16-bit unicode
2. 8-bit chars in some encoding
3. binary data.
4. serialised objects.

Then you can ask the File I/O amanuensis to generate the necessary
code to read it.

See http://mindprod.com/fileio.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

ak 06-17-2004 07:32 AM

Re: JNI byte array to string
 
readUTF() doesn't create UTF, but read data wiich is in UTF format.
see http://java.sun.com/j2se/1.3/docs/ap....html#readUTF()

try {
DataInputStream dis = new DataInputStream(new
ByteArrayInputStream(unicode_byte_array));
orig = dis.readUTF();
}
catch (IOException e)
{
//System.out.println(e);
}


--
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader



Roedy Green 06-17-2004 03:21 PM

Re: JNI byte array to string
 
On Thu, 17 Jun 2004 09:32:38 +0200, "ak" <spam@imagero.com> wrote or
quoted :

>readUTF() doesn't create UTF, but read data wiich is in UTF format.


UTF is not just unicode-8. It is a special binary format with counted
strings. It is not designed to be human readable.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Michael Borgwardt 06-17-2004 03:47 PM

Re: JNI byte array to string
 
Roedy Green wrote:
>>readUTF() doesn't create UTF, but read data wiich is in UTF format.

>
>
> UTF is not just unicode-8. It is a special binary format with counted
> strings. It is not designed to be human readable.


There's no such thing as "unicode-8", and UTF-8 is exactly as "human readable" as
ASCII (to which it is downwards-compatible) or any other text encoding.
The readUTF() method simply expects a sequence of UTF-8 encoded characters
prepended by two bytes specifying the length of the sequence.

static 06-17-2004 04:26 PM

Re: JNI byte array to string
 
guys I tried the readUTF() but if I print out orig, the output doesn't
match the output from the unicode_byte_array. The whole string seems
like it shrunk the byte array down. I would like to print the String
and have the output match the byte array. I also tried writing the
data to a file and reading it with

InputStream ba = new FileInputStream("test");
DataInputStream dis = new DataInputStream(ba);
orig = dis.readUTF();

but when I print out orig, the output is different. I'll be glad to
mail you my data file which is about 1830 bytes for you to try.

Thanks so much for the input. Any other ideas?

Ashley

"ak" <spam@imagero.com> wrote in message news:<carhef$u4k$1@online.de>...
> readUTF() doesn't create UTF, but read data wiich is in UTF format.
> see http://java.sun.com/j2se/1.3/docs/ap....html#readUTF()
>
> try {
> DataInputStream dis = new DataInputStream(new
> ByteArrayInputStream(unicode_byte_array));
> orig = dis.readUTF();
> }
> catch (IOException e)
> {
> //System.out.println(e);
> }


Roedy Green 06-17-2004 05:02 PM

Re: JNI byte array to string
 
On Thu, 17 Jun 2004 17:47:57 +0200, Michael Borgwardt
<brazil@brazils-animeland.de> wrote or quoted :

>
>There's no such thing as "unicode-8", and UTF-8 is exactly as "human readable" as
>ASCII (to which it is downwards-compatible) or any other text encoding.
>The readUTF() method simply expects a sequence of UTF-8 encoded characters
>prepended by two bytes specifying the length of the sequence.


People try to use writeUTF to create human-readable files. They are
not because of the length fields.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

ak 06-17-2004 06:32 PM

Re: JNI byte array to string
 
> but when I print out orig, the output is different. I'll be glad to
> mail you my data file which is about 1830 bytes for you to try.
>


post an attachment, and dont forget to post also original string.

--
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader




All times are GMT. The time now is 05:06 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.