Results 1 to 6 of 6
  1. #1
    sandeepvreddy is offline Member
    Join Date
    Sep 2008
    Posts
    2
    Rep Power
    0

    Default String/sentence to unicode convertion

    Hi,
    my requirement is to convert a string/sentence in a input file to Unicode and write into another file
    eg:
    "abc" to (& # 97;& # 98; & # 99; )
    thank you
    Last edited by sandeepvreddy; 09-22-2008 at 07:20 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I thought all Strings were already by default "unicode" (UTF-16).

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quite similar question is discuss here in this forum few days back. Search on that, you can find the solution there.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    See HTML Character Reference.
    Java Code:
    public class Test {
        public static void main(String[] args) {
            String s = "hello world";
            for(int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                int n = (int)c;
                String hex = Integer.toHexString(n);
                String unicode = "\\u" + padAhead(hex);
                String characterReference = "&#" + n + ";";
                System.out.printf("c = %s  n = %3d  hex = %s  " +
                                  "unicode = %s  characterReference = %s%n",
                                   c, n, hex, unicode, characterReference);
            }
    
            // Another possibility:
            char[] chars = s.toCharArray();
            for(int i = 0; i < chars.length; i++) {
                System.out.printf("codePointAt(%2d) = %d%n", i,
                                   Character.codePointAt(chars, i));
            }
        }
    
        private static String padAhead(String s) {
            int length = s.length();
            while(s.length() < 4) {
                s = "0" + s;
            }
            return s;
        }
    }
    Last edited by hardwired; 09-22-2008 at 09:58 AM. Reason: forgot the ampersand

  5. #5
    janne is offline Member
    Join Date
    Nov 2008
    Location
    Germany, Dresden
    Posts
    5
    Rep Power
    0

    Default how to convert a numerical value (position) to unicode charater

    Hi all,

    I'm going to write a small program that read in a txt configuration file which contains numerical values. These values represent a charater and must be transformed into a String. There are a function or class which do that for me?

    e.g:
    Java Code:
    Integer i = Integer.decode("42");  // position of char "*" is 42
    byte[] chr = new byte[1];
    chr[0] = i.byteValue();
    String str = new String(chr, "utf-8");
    System.out.println(str);              // print "*"
    Well, this example works not for values greater than 255. For example if I use (Euro sign, \u20ac, 8364). How can I transform a position in its string representation?

    Many thanks, Janne

  6. #6
    janne is offline Member
    Join Date
    Nov 2008
    Location
    Germany, Dresden
    Posts
    5
    Rep Power
    0

    Default

    Sometimes I don't see the wood for the threes. The solution is quite simple because
    Java is smart enough to do a proper cast.

    Java Code:
    int position = 8364;
    String specialCharacter = "" + (char)position;
    System.out.println("charater of position "+position+" is: " + specialCharacter);

    Thx, Janne
    Last edited by janne; 11-20-2008 at 03:57 PM.

Similar Threads

  1. stiring to unicode convertion
    By sandeepvreddy in forum New To Java
    Replies: 2
    Last Post: 09-20-2008, 05:08 PM
  2. How to Draw Unicode String in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 11:15 PM
  3. Problem with Image Convertion
    By aptivo in forum Java 2D
    Replies: 0
    Last Post: 06-09-2008, 11:15 PM
  4. Date convertion in java
    By Preethi in forum New To Java
    Replies: 25
    Last Post: 05-16-2008, 05:11 PM
  5. How to extract info from a sentence
    By luisarca in forum XML
    Replies: 1
    Last Post: 06-07-2007, 05:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •