Converting String to unicode encoded string

Hi,

I would like to convert non-ascii characters in a String or the whole string to unicode encoded character.

For example:

If i have some japanese character, I would like it to be converted to /uxxxx

How can i do this? I don't want the exact character, as I am able to get that using getBytes("encoding format"). All i want is code point representation of the non ascii unicode characters.

Any help to do this will be appreciated.

Thanks in advance.

[495 byte] By [DeepSofta] at [2007-11-26 22:59:10]
# 1
My subject line might not be very clear so changing it here. I know that java String is already unicode.
DeepSofta at 2007-7-10 12:25:27 > top of Java-index,Java Essentials,Java Programming...
# 2
Is there any equivalent method of Character.codePointAt() in java 1.4.2Again I am trying to get unicode code point value from String (Unicode string).Thanks
DeepSofta at 2007-7-10 12:25:27 > top of Java-index,Java Essentials,Java Programming...
# 3
Casting a char to an int is probably at the root of what you are asking.And converting a single char whose hexadecimal value is abcd into the six-character string "\uabcd" is unlikely to be a useful thing to do.
DrClapa at 2007-7-10 12:25:27 > top of Java-index,Java Essentials,Java Programming...
# 4

I tried to do what that but I am not sure whether that is right or not.

String inputStr = "some non ascii string";

char[] charArray = inputStr.toCharArray();

int code;

StringBuffer sb = new StringBuffer();

for(int i = 0; i < charArray.length; i++)

{

code = (int) charArray;

String hexString = Integer.toHexString( code );

sb.append(hexString);

}

System.out.println("Code point is "+sb.toString());

My above code does not work as expected. Could you please tell me where i am goofing?

Thanks!

DeepSofta at 2007-7-10 12:25:27 > top of Java-index,Java Essentials,Java Programming...
# 5
A Unicode escape has to contain four hex digits, but toHexString() only uses as many digits as it needs to. You could add leading zeroes, but if you're running JDK 1.5 or later, there's an easier way. String uEscape = String.format("\\u%4s", code);
uncle_alicea at 2007-7-10 12:25:27 > top of Java-index,Java Essentials,Java Programming...