Solved

UTF-8 Character to Unicode

Posted on 2009-07-02
9
243 Views
Last Modified: 2012-05-07
Hi Experts;

I have a UTF-8 Character that I need to convert to unicode so that it can be shown correctly in a web browser:

I have this character ® that should be converted to = '®' so that the browser can interpret it correctly. Is there an easier way to convert it in Java?

Thank you.
0
Comment
Question by:simple21
  • 3
  • 3
  • 2
9 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24769605
try this:

String escaped = "&#"+Character.digit(c, 10)+";";
0
 

Author Comment

by:simple21
ID: 24770248
Hi Objects,

It returns: '&#-1;' What I need is: '®' for ®, here is where I get the table:
http://www.tony-franks.co.uk/UTF-8.htm

Here is my code:



public class CharacterEncodingUtil {
 

	public CharacterEncodingUtil()  {

        System.out.println(toUnicodeEscaped('®'));

	}

	

	public static String toUnicodeEscaped(char c)  {

		String unicodeChar = "&#" + Character.digit(c, 10) +";";

        return unicodeChar;

  }

	public static void main(String[] args) {

		new CharacterEncodingUtil();

	}

}

Open in new window

0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 24770265
actually this should be all you need

                String unicodeChar = "&#" + ((int)c) +";";
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 24770273
Try
String unicodeChar = "&#" + (int)c +";";

Open in new window

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:simple21
ID: 24770536
Thanks for all the help one last question, if you may. Which class can I use to check if there is a UTF-8 character in a string?


String utf = "This is a test company ® private limited".

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24770551
What you mean is 'how can i check if there are any 'special' characters in a string?' They're all UTF-8 essentially. A rough but workable way is to examine, where 'c' is the character in question,

boolean special = c >= 0x7F;
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24770552
>>where 'c' is the character in question,

i.e. 'c' is of type char
0
 
LVL 92

Expert Comment

by:objects
ID: 24771000
> Which class can I use to check if there is a UTF-8 character in a string?

there isn't anything standard.  If you know what special chars you are expecting then you can use replaceAll() to replace them.
commans lang also has a variety of methods for escaping strings
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now