Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

How to convert utf-8 bytes into a java string?

Thread view: 
au.danji@gmail.com - 13 Nov 2007 17:04 GMT
Hi, I need to received the special bytes from a URL and convert them
into a java string, can anyone help me
about this? most appreciated for your help.

Eg. http://www..../search_str=%E6%84%9F%E5%86%92

I need convert the utf-8 format %E6%84%9F%E5%86%92 into a java string,
thanks a lot!
Thomas Fritsch - 13 Nov 2007 17:51 GMT
au.danji@gmail.com schrieb:
> Hi, I need to received the special bytes from a URL and convert them
> into a java string, can anyone help me
[quoted text clipped - 4 lines]
> I need convert the utf-8 format %E6%84%9F%E5%86%92 into a java string,
> thanks a lot!

String s =
URLDecoder.decode("http://www..../search_str=%E6%84%9F%E5%86%92","UTF-8");

It produces a String with 2 chinese characters after "search_str=",
namely "http://www..../search_str=\u611F\u5192" when written in Java's
\uxxxx syntax.

Signature

Thomas

John Maline - 13 Nov 2007 18:18 GMT
> Hi, I need to received the special bytes from a URL and convert them
> into a java string, can anyone help me
[quoted text clipped - 4 lines]
> I need convert the utf-8 format %E6%84%9F%E5%86%92 into a java string,
> thanks a lot!

There's a String constructor that takes an array of bytes and the name
of the character set.
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#String(byte[],%20j
ava.lang.String
)

So it'd be...
String s = new String(myByteArray, "UTF8");

JavaDoc is your friend.  If you want to make an instance of class X, the
first place to look is the list of constructors and static (factory)
methods in the javadocs for class X.  Not always the right answer for a
question like that, but it's the place to start...

John
Roedy Green - 13 Nov 2007 22:10 GMT
On Tue, 13 Nov 2007 17:04:00 -0000, "au.danji@gmail.com"
<au.danji@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>Hi, I need to received the special bytes from a URL and convert them
>into a java string, can anyone help me
>about this? most appreciated for your help.

see http://mindprod.com/jgloss/conversion.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 13 Nov 2007 22:20 GMT
On Tue, 13 Nov 2007 17:04:00 -0000, "au.danji@gmail.com"
<au.danji@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>I need convert the utf-8 format %E6%84%9F%E5%86%92 into a java string,
>thanks a lot!

That looks like some flavour of armoured utf-8. See
http://mindprod.com/jgloss/armouring.html

It is probably URL-encoded.
see http://mindprod.com/jgloss/urlencoded.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Steven Simpson - 13 Nov 2007 23:01 GMT
> Eg. http://www..../search_str=%E6%84%9F%E5%86%92
>
> I need convert the utf-8 format %E6%84%9F%E5%86%92 into a java string,
> thanks a lot!

Shove it in a java.net.URI, and extract the parts you want.  This should
ensure that extraction occurs before decoding, since you don't want to
misinterpret an encoded character as a separator.

For me, this program appears to do the job:

import java.net.*;

public class Decode {
 public static void main(String[] args) throws Exception {
   for (String a : args) {
     URI u = URI.create(a);
     System.out.println("Arg: " + a);
     System.out.println("URI: " + u);
     System.out.println("Scheme: " + u.getScheme());
     System.out.println("Authority: " + u.getAuthority());
     System.out.println("UserInfo: " + u.getUserInfo());
     System.out.println("Host: " + u.getHost());
     System.out.println("Port: " + u.getPort());
     System.out.println("Path: " + u.getPath());
     System.out.println("Query: " + u.getQuery());
     System.out.println("Fragment: " + u.getFragment());
   }
 }
}

Signature

ss at comp dot lancs dot ac dot uk                                     |



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.