?
Solved

UTF-8 Character to Unicode

Posted on 2009-07-02
9
Medium Priority
?
250 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
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!

 
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

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

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
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 Month10 days, 6 hours left to enroll

764 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