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

A

au.danji

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=感冒

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

Thomas Fritsch

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=感冒

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=感冒","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.
 
J

John Maline

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=感冒

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[], java.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
 
S

Steven Simpson

Eg. http://www..../search_str=感冒

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());
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top