Solved

UTF-8 Character to Unicode

Posted on 2009-07-02
9
248 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
[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
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate example using maven 12 82
ejb example issues 3 55
Why doesn't this text field show up on my Applet frame? 2 28
Coding for the first time 9 70
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

730 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