Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

UTF-8 Character to Unicode

Posted on 2009-07-02
9
Medium Priority
?
251 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 1000 total points
ID: 24770265
actually this should be all you need

                String unicodeChar = "&#" + ((int)c) +";";
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

604 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