How to convert from string to unicode

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shalinianthony
    New Member
    • Mar 2008
    • 4

    #1

    How to convert from string to unicode

    language:Java

    Code:
    String code="\u0057";
    System.out.println(code);
    The output for this code is:W


    But if i want to break it and then use it, like the below, it doesnt consider it as a unicode....it takes it as a string.

    Code:
    String code="\\u";
    code+="0057"; 
    System.out.println(code)
    Output:\u0057

    The purspose is that i would like to generate random numbers(for the last 2 digits of the unicode),basica lly to generate different unicodes.
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    This approach doesn't work.
    If you give something like "\u0061", then the compiler does not store 6 characters for the string, but it converts it to one character (in this example "a") and stores it. So the above would be the same as if I typed "a" instead, it's only a different way of writing. Every character is stored as 2 bytes.
    That means you cannot do it in a "dynamic" way you were trying during compile time, only during runtime. The compiler needs fixed values. This is why "\u" will throw you a compile exception. But using "\\u" is no solution, because this stores 2 charaters: the backslash and the "u".
    Accordingly, "\\u0032" will be stored as 6 characters;

    You must approact the solution in a different way:
    You must first make an integer that contains the random number (=unicode value of the desired character)
    Then you use the function String(byte[] bytes) to convert it to a String.
    here is the code draft:

    Code:
    Math.Random random = new Math.Random();
    int randomUnicode = random.nextInt();
    byte[] bytes = new byte[2];
    bytes[0] = 0;
    bytes[1] = (byte) randomUnicode;
    String final = new String(bytes);
    Originally posted by shalinianthony
    language:Java

    Code:
    String code="\u0057";
    System.out.println(code);
    The output for this code is:W


    But if i want to break it and then use it, like the below, it doesnt consider it as a unicode....it takes it as a string.

    Code:
    String code="\\u";
    code+="0057"; 
    System.out.println(code)
    Output:\u0057

    The purspose is that i would like to generate random numbers(for the last 2 digits of the unicode),basica lly to generate different unicodes.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by chaarmann
      Code:
      Math.Random random = new Math.Random();
      int randomUnicode = random.nextInt();
      byte[] bytes = new byte[2];
      bytes[0] = 0;
      bytes[1] = (byte) randomUnicode;
      String final = new String(bytes);
      That assumes a big endian order for the current decoding method; why not simply
      do this:

      [code=java]
      Random random = new Random();
      String final= ""+(char)random .nexInt(256);
      [/code]

      kind regards,

      Jos

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        Jos, good idea!. But you can improve your code by using:

        Code:
        Random random = new Random();
        String finalString = String.valueOf((char)random.nextInt(256));
        In this case, there is no need to create an empty string object and then create a second one that contains the character and then a third one that concatenates both of them. It just creates one. This leads to better performance.

        By the way, my original code and Jos code was not tested, to provide a solution quickly. Now I testde them and I figured out that both had several bugs:
        Jos solution:
        - "final" is a reserved word, you cannot use this as a variable name.
        - "nexInt" was mistyped and should be "nextInt".

        My solution:
        - Random is in package "java.util" not "java.math" . I got it wrong because I was doing a lot of javascript and there it's Math.Random().
        - "final" is a reserved word, you cannot use this as a variable name.

        Jos, a question about your remark on bigEndian:
        my JDK1.4 is using it. Isn't that always the case for the unicode that Java uses? Can you switch it somehow (with a javac option or so?) And if so, can you inquire which one is used? It would be interesting to enhance my code, so it doesn't store the bytes in the wrong order. And I can't use your code, because I am doing some arithmetics on the lower and upper byte in my orginal code at work, and my array contains more than one character and irregular sizes for a character (UTF-8 can contain one, two, three or four bytes per character).

        Comment

        Working...