Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Unicode problem

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Marshal
Posts: 27374
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic