Solved

UTF-8 Character to Unicode

Posted on 2009-07-02
9
246 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 86

Assisted Solution

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

Open in new window

0
 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
printing a file in reverse order is easy in recursion rather than in iteration 3 56
Weekend adv creator 3 38
diffSum example 4 37
hibernate jars 4 30
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.

773 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