A small study of Java Unicode escape characters

Source: Internet
Author: User
Tags return stringbuffer
Escape character


all of the escape characters are preceded by ' \ '


Second character


0-9: Octal


u: is a Unicode turn, fixed length to 6-bit


Other: One of the b,t,n,f,r in the following letters, ", \


is not satisfied, a compilation error is generated.








of course, the provision of octal is also to be compatible with the C language.




The
B,t,n,f,r is the control character. The meaning of the book is to describe how the sender of the data stream wants the information to be formatted or represented.





Unicode escape characters are very interesting


it can be written anywhere in the code, as long as the escape is legal.


For example:





int c=0\u003b


The above code can be compiled through, equivalent to int c=0; \u003b is the '; ' The Unicode code





The above example is not convincing, so I wrote the following code to convert a string to a Unicode character


then you can put the printed code in the Egghello.java and run it, and the result is exciting.








/*


* Author: Egg DATE:2004/11/10 string converted to Unicode


*/


public class Asc2unicode {


private StringBuffer ch;





private StringBuffer tch;





Asc2unicode (StringBuffer s) {


ch = s;


if (ch.length () > 0)


tch = new StringBuffer (1000);


}





void transform () {


if (Ch.length () < 1)


return;


for (int i = 0; i < ch.length (); i++) {


((int) ch.charat (0)). toString ();


Tch.append (New StringBuffer ("\\u00"));


Tch.append (New StringBuffer (integer.tohexstring (int) ch


. CharAt (i))));


}


}





StringBuffer getstr () {


return tch;


}





public static void Main (string[] argv) {


asc2unicode au = new Asc2unicode (new StringBuffer (New String (


"public class egghello{"


+ "public static void Main (string[] argv) {"


+ "System.out.println" (\ "hello,world\"); "+"} "+"} "


)


));


Au.transform ();


System.out.println (Au.getstr ());


}


}








print as follows:


\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0063\u006c\u0061\u0073\u0073\u0020\u0045\u0067\u0067\u0048\ U0065\u006c\u006c\u006f\u007b\u0020\u0020\u0020\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0073\u0074\u0061\ U0074\u0069\u0063\u0020\u0076\u006f\u0069\u0064\u0020\u006d\u0061\u0069\u006e\u0028\u0053\u0074\u0072\u0069\ U006e\u0067\u005b\u005d\u0020\u0061\u0072\u0067\u0076\u0029\u007b\u0053\u0079\u0073\u0074\u0065\u006d\u002e\ u006f\u0075\u0074\u002e\u0070\u0072\u0069\u006e\u0074\u006c\u006e\u0028\u0022\u0048\u0065\u006c\u006c\u006f\ u002c\u0057\u006f\u0072\u006c\u0064\u0022\u0029\u003b\u007d\u007d











feels a bit like the encrypted code! Of course it's all a self-deception ...


But at first it seems to be more giddy.





Unicode Escape first study here, drink some water, play games ...

















Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.