It's not a secret anymore!*
The moose likes Java in General and the fly likes ByteArrayOutputStream to String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ByteArrayOutputStream to String" Watch "ByteArrayOutputStream to String" New topic
Author

ByteArrayOutputStream to String

Shobha Korrapati
Greenhorn

Joined: Aug 08, 2007
Posts: 13
Hi All,

I am getting a field in ByrArrayOutpuStream . I need to convert it into String and display.Actually the data in the field has japanese characters.
I tried to convert using the method toString("UTF-8").But it is displaying junk characters
Can anyone help me out .

Thanks in advance,
Shobha


SCJP , SCWCD
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19680
    
  19

Maybe use a different encoding, that does support Japanese.

Also remember that the Windows / Unix command line also does not understand some character sets like Japanese. I even had a Linux command line that didn't recognise the "�" character. If you print to System.out you may never get it to work. Does it work in a JLabel, or a JTextArea?

Also, do you have support for the Japanese character set on you Windows / Unix / ...? If not, you won't get to see any Japanese characters either.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Shobha Korrapati
Greenhorn

Joined: Aug 08, 2007
Posts: 13
Actually , I am working on japanese OS
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19680
    
  19

Check http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html]http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html]http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html

You should try one of the encodings that supports Japanese, like MS932, EUC_JP or ISO2022JP.
Shobha Korrapati
Greenhorn

Joined: Aug 08, 2007
Posts: 13
I tried with MS932 EUC-JP Shift_JIS ISO-2022-JP .But its not working
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Well, you probably first should find out how the stream's content is encoded.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
i would try UTF-16 as well. But yes, it's really best if whoever stored these bytes can tell you what encoding they use. Otherwise you can just find a list of all the encodings commonly used for Japanese, and try them all.


"I'm not back." - Bill Harding, Twister
Shobha Korrapati
Greenhorn

Joined: Aug 08, 2007
Posts: 13
I am getting the data from a UTF-8 database.
I tried with common encoding we use like UTF-8, SHIFT_JIS , but its not working.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Shobha Korrapati:
I am getting the data from a UTF-8 database.


Who is writing to that database?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

I also would go with Jim. First, try to find out what encoding has been used. If not, then try with the available but related options.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
What is the SQL datatype of the field, and how did you get the data into a ByteArrayOutputStream? I'm guessing it was a BLOB, in which case it's irrelevant that the database uses UTF-8 - you need to know what encoding was used by whoever created the BLOB.
H. Hall
Greenhorn

Joined: Sep 14, 2007
Posts: 8
I think your problem is that java Strings are unicode format and utf8 is not.

Why don't you take a look at the DataInputStream class. The following method might be helpful:

static String readUTF(DataInput in)

Reads from the stream in a representation of a Unicode character string encoded in Java modified UTF-8 format; this string of characters is then returned as a String.

Cheers
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ByteArrayOutputStream to String