• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5027
  • Last Modified:

Java Unicode Currency Symbol

Hi All:

Background information:
Unicode Hex Value for Dollar Symbol ($) : 0x24
Unicode Decimal Value for Dollar Symbol ($) : 36
Unicode notation : '\u0024'

I am getting an input string "$" from browser window, i am supposed to parse it and print "$" in the reports.
Is there any java api, which gets the Unicode hex/decimal value and returns the symbol?

Actually I am able to convert "$"  to "\u0024".
However, jvm distinguishes between "\u0024" object and "\u0024" literal. what i need is $ symbol but what i get is \u0024 unicode string...

following code snippet describes the problem more accurately.

import java.text.*;
public class UnicodeSucks
{
      public static void main(String[] args)
      {

                  //char ch = '\u00A3';
                //Character.UnicodeBlock block = Character.UnicodeBlock.of(ch);
                  //System.out.println(Character.UnicodeBlock.of('\u0024'));
                  String toCurrSymbol="$";

                  String tempCurrSymbol = toCurrSymbol.trim().substring(3,toCurrSymbol.length()-1);

                  String currSymbol="\\u";
                  for(int i=0;i<4-tempCurrSymbol.length();i++)
                  {
                        currSymbol=currSymbol+"0";
                  }
                  currSymbol+=tempCurrSymbol;

                  System.out.println(currSymbol.intern());
                  System.out.println("\u0024".intern());

                  if(currSymbol.intern().equals("\u0024".intern()))
                  {
                        System.out.println("This doesnt get printed :-(");
                  }
                  if("$".intern().equals("\u0024".intern()))
                  {
                        System.out.println("This is the problem");
                  }
      }
}



0
tapasvi
Asked:
tapasvi
1 Solution
 
girionisCommented:
> I am getting an input string "&#x24;" from browser window, i am supposed
> to parse it and print "$" in the reports.

Browsers are supposed to automatically print the $ symbol. What encoding do you have in your browser? Where are you reading the $ symbol from? It might be that the encoding used to represent the symbol is different than the one a browser understands.

0
 
tapasviAuthor Commented:
>>Browsers are supposed to automatically print the $ symbol.
It does.
Sorry I didnt mention this earlier, but the input is from the jsp whereas output is going to be tool specific report (loaded by applet)
so, problem doesnt arise if the output is going to a simple jsp page..it automatically converts "&#x24;" (hex) or "&#36;" (decimal) to $.
but what i need is to display the currency symbol in an applet report.

Now, you may think that this is too insufficient a description because tool may have some proprietary ways..

but if instead of the generated "\u0024" throught the above code snippet, if I use hard-coded "\u0024" it works.
because JVM interprets "\u0024" as $

thats why the code below works:
          if("$".intern().equals("\u0024".intern()))
               {
                    System.out.println("This is the problem");
               }

So, the problem arises due to jvm not interpreting programmatically generated string "\u0024" as $. it just takes it literally  and doesnt yield "$".


0
 
girionisCommented:
Can you not simply do (if "symbol" holds the unicode/decimal interpretation)


if(symbol.equals("\\u0024") || symbol.equals("&#x24;") || symbol.equals("&#36;"))
               {
                    System.out.println("$");
               }
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
tapasviAuthor Commented:
well, what if the currency is not $..
it can be *any* currency
0
 
ashok3sepCommented:
char display[]=new char[200];
int i,j,c,val,t;
c=-1;
String line; //-------> Here You input the Hexadecimal Value
for(i=0; i<line.length; ++i)
{   if(line.charAt(i)=='0' && line.charAt(i+1)=='x')
    {   val=0;
        for(j=i+2; (t=Character.digit(line.charAt(j),16))!=-1; ++j)
        {   val*=16;
            val+=t;
        }
        i=j;
        display[++c]=(char)val;
    }
}

regards,
Freedom
0
 
girionisCommented:
>well, what if the currency is not $..
>it can be *any* currency

Not sure if this will work but if you know that you will always get the symbols like this you could do:

String symbol = "&#x24;";
String unicodeSymbol = "\\u00" + symbol.charAt(3) + symbol.charAt(4);
System.out.println(unicodeSymbol).
0
 
objectsCommented:
you need to parse it yourself, I remember posting some code to do it ages ago but unfortunately cannot find it.
Pretty straight forward though.
0
 
tapasviAuthor Commented:
>String symbol = "&#x24;";
>String unicodeSymbol = "\\u00" + symbol.charAt(3) + symbol.charAt(4);
>System.out.println(unicodeSymbol).

this prints \u0024 and not $

:-(
0
 
girionisCommented:
Maybe this: http://www.javaspecialists.co.za/archive/Issue036.html will give you some ideas. It is an implementation that allows you to enter decimal values and convert them to the corresponding unicode character.
0
 
tapasviAuthor Commented:
I got the answer :


            toCurrSymbol = "&#24;".trim().substring(2,"&#24;".length()-1);
            toCurrSymbol =String.valueOf((char) Integer.decode(toCurrSymbol).intValue());

My recommendation:
Delete - Refund
0
 
moduloCommented:
PAQed with points refunded (100)

modulo
Community Support Moderator
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now