LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-09-2006, 07:49 PM   #1
rusty_turkey
LQ Newbie
 
Registered: Oct 2005
Location: Queensland, Australia
Distribution: Solaris
Posts: 14

Rep: Reputation: 0
Unhappy Display Unicode Code from a String in Java?


Hi,

I'm trying to make a simple program that will display the corresponding unicode for a string of text.
eg.

String text = "some text";

System.out.println(convertToUnicode(text));

The program should display the unicode that represents the given string, such as:
\u2340\u2432\u2830...etc

Is there any way to do this without manually programming like this:
case 'a': return "\u2345"; // or whatever the code for 'a' is
 
Old 08-09-2006, 08:39 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
I am not a java expert, but looking at the latest API, why not use something like Character.codePointAt() function. The result should probably be displayed in hex (for example with printf("\\u%04x\n", blah)).
 
Old 08-09-2006, 08:50 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I'm not quite certain what you have managed to achieve so far (and my Java is very rusty) but you should be able to take each character and using type conversion store it as a number. But maybe that's not your problem?
Code:
    String test = "ABCD";
    for ( int i = 0; i < test.length(); ++i )
    {
        char c = test.charAt( i );
        int j = (int) c;
        System.out.println(j);
    }
 
Old 08-10-2006, 05:36 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by graemef
I'm not quite certain what you have managed to achieve so far (and my Java is very rusty) but you should be able to take each character and using type conversion store it as a number.
While I think this should also work fine (except maybe for characters outside of the BMP: IIRC, in Java, ints are 4 bytes and chars are 2 bytes), I don't think that's the "Java Way". Casting is discouraged in java in preference to using classes' methods.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting a class From a String in Java MRMadhav Programming 5 02-18-2006 05:50 AM
strip number from string (c code) alaios Programming 8 09-26-2005 12:47 AM
printing unicode characters in JAVA Armand Programming 8 03-06-2004 07:51 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM
C; playing with string collective code TheLinuxDuck Programming 9 07-14-2003 05:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration