• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unicode problem

 
Marcus Liu
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the unicode of 'A' is '\u0041';

org.apache.common.lang.CharUtils.unicodeEscaped('A') = \u0041 ,

System.out.print(new String("\u0041")); --> result is 'A'

but
1). System.out.println(new String(CharUtils.unicodeEscaped('A'))) --> resutl is \u0041 , why not 'A'?
2) CharUtils.unicodeEscaped('A').equals(new String("\u0041")) --> result is false , why not true ?

who know it ? thx
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're getting confused between Strings and string literals.

In a string literal \u0041 means the character A.

But CharUtils.unicodeEscaped('A') returns a String whose contents are the characters \, u, 0, 0, 4, and 1. That will not be unescaped when you use it as the argument of new String().
 
Marcus Liu
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
You're getting confused between Strings and string literals.

In a string literal \u0041 means the character A.

But CharUtils.unicodeEscaped('A') returns a String whose contents are the characters \, u, 0, 0, 4, and 1. That will not be unescaped when you use it as the argument of new String().


Thx your reply , I have knew a little idea.
In my test :

CharUtils.unicodeEscaped('A').equals("\\u0041") is true

so

is there any way to convert "\\u0041" to "\u0041" ?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marcus Liu:
is there any way to convert "\\u0041" to "\u0041" ?


Sorry, you're going to have to explain more clearly and carefully what you want to do here. Explain what's the contents of a String and what's a string literal.

Or perhaps take a step back and explain what the higher-level thing is that you are trying to achieve.
 
Marcus Liu
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry .

I meet a little trouble with the below :

char sourceChar = 'A';
String charUnicode = org.apache.common.lang.CharUtils.unicodeEscaped('A'); (comment : charUnicode == "\\u0041")

String realString = new String(charUnicode);

I want to get the 'realString' is 'A'.

do I explain it clearly ?
 
Paul Clapham
Sheriff
Pie
Posts: 20735
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't the CharUtils class have another method for converting back? I don't have it here and I don't really feel like hunting down its documentation on the Internet.

If it doesn't, then you can convert the last four of those six characters to a char like this:But if you think you need to do this, and you aren't writing a Java compiler or parser, you probably don't really need to do this.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic