?
Solved

Java Unicode Currency Symbol

Posted on 2005-03-03
13
Medium Priority
?
4,871 Views
Last Modified: 2010-05-18
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
Comment
Question by:tapasvi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 13448607
> 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
 
LVL 4

Author Comment

by:tapasvi
ID: 13448648
>>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
 
LVL 35

Expert Comment

by:girionis
ID: 13448683
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 4

Author Comment

by:tapasvi
ID: 13448692
well, what if the currency is not $..
it can be *any* currency
0
 
LVL 5

Expert Comment

by:ashok3sep
ID: 13449472
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
 
LVL 35

Expert Comment

by:girionis
ID: 13450451
>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
 
LVL 92

Expert Comment

by:objects
ID: 13453398
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
 
LVL 4

Author Comment

by:tapasvi
ID: 13456788
>String symbol = "&#x24;";
>String unicodeSymbol = "\\u00" + symbol.charAt(3) + symbol.charAt(4);
>System.out.println(unicodeSymbol).

this prints \u0024 and not $

:-(
0
 
LVL 35

Expert Comment

by:girionis
ID: 13457113
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
 
LVL 4

Author Comment

by:tapasvi
ID: 13660593
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
 

Accepted Solution

by:
modulo earned 0 total points
ID: 13747249
PAQed with points refunded (100)

modulo
Community Support Moderator
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question