2
 public class TestU {
    public static void main(String[] args) {
        String str = "\u0001";
        System.out.println("str-->"+str);
        System.out.println("arg[0]-->"+args[0]);
    }
}

Output :

str-->^A
arg[0]-->\u0001

I am passing arg[0] as \u0001

I executed this code in linux, the command line variable is not taken as unicode special character.

Peter Wippermann
  • 4,125
  • 5
  • 35
  • 48
vinayak_narune
  • 737
  • 1
  • 10
  • 26
  • Your commandline argument takes string array as input and hence treat it as a String. – SMA Dec 26 '15 at 10:29
  • http://stackoverflow.com/questions/11145681/how-to-convert-a-string-with-unicode-encoding-to-a-string-of-letters – Marcinek Dec 26 '15 at 10:29

1 Answers1

2

The argument you pass from command line is not actually unicode character but it's a String of unicode character which is escaped with \. Ultimately, your String will become \\u0001 and that's why it is printing \u0001. Same way, if you enter \ as a command line argument it will become \\ to escape your backward slash.

While the String you have declared in main is actually unicode character.

String escapedstring = "\\u0001";//in args[0]
String unicodeChar = "\u0001";// in str

So, now you want \\u0001 to be converted into \u0001 and there are lot of ways to achieve that. i.e. you can use StringEscapeUtils#unescapeJava method of utility or you can also try following way.

String str = "\\u0001";
char unicodeChar = (char) Integer.parseInt(str.substring(2));
System.out.println(unicodeChar);

NOTE : You can find other ways to convert unicode String to unicode characters in following question.(Already provided in comment by Marcinek)

Community
  • 1
  • 1
akash
  • 22,664
  • 11
  • 59
  • 87
  • Ok. Somewhere I am getting this values \u0001 I need this to be behave as unicode and not as string \u0001 , beacause I need to use this special character as delimieter to write into file but presently its writing as A\u0001B it should be A^AB where A and B is the data for two columns respectively – vinayak_narune Dec 26 '15 at 10:59