Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print string literal unicode as the actual character

Tags:

java

unicode

In my Java application I have been passed in a string that looks like this:

"\u00a5123"

When printing that string into the console, I get the same string as the output (as expected).

However, I want to print that out by having the unicode converted into the actual yen symbol (\u00a5 -> yen symbol) - how would I go about doing this?

i.e. so it looks like this: "[yen symbol]123"

like image 454
digiarnie Avatar asked Sep 10 '09 00:09

digiarnie


2 Answers

I wrote a little program:

public static void main(String[] args) {
    System.out.println("\u00a5123");
}

It's output:

¥123

i.e. it output exactly what you stated in your post. I am not sure there is not something else going on. What version of Java are you using?

edit:

In response to your clarification, there are a couple of different techniques. The most straightforward is to look for a "\u" followed by 4 hex-code characters, extract that piece and replace with a unicode version with the hexcode (using the Character class). This of course assumes the string will not have a \u in front of it.

I am not aware of any particular system to parse the String as though it was an encoded Java String.

like image 96
aperkins Avatar answered Oct 06 '22 19:10

aperkins


As has been mentioned before, these strings will have to be parsed to get the desired result.

  1. Tokenize the string by using \u as separator. For example: \u63A5\u53D7 => { "63A5", "53D7" }

  2. Process these strings as follows:

    String hex = "63A5";
    int intValue = Integer.parseInt(hex, 16);
    System.out.println((char)intValue);
    
like image 43
Abhinav Maheshwari Avatar answered Oct 06 '22 19:10

Abhinav Maheshwari