1 Public classCode {2 3 Public Static voidMain (string[] args)throwsexception{4 //TODO auto-generated Method Stub5String s= "Practice By Yourself";6 byte[] byte1=s.getbytes ();//Converts a string to a character array, using the project's default encoding7 for(byteb:byte1) {8 //The byte is displayed in 16 binary mode9System.out.print (integer.tohexstring (B&0XFF) + "");Ten } One System.out.println (); A //jbk Chinese occupies two bytes, English occupies one byte - byte[] Byte2=s.getbytes ("GBK"); - for(byteb:byte2) { theSystem.out.print (integer.tohexstring (B&0XFF) + ""); - } - //one of the utf-8 Chinese occupies three bytes, English occupies one byte - System.out.println (); + byte[] Byte3=s.getbytes ("Utf-8"); - for(byteb:byte3) { +System.out.print (integer.tohexstring (B&0XFF) + ""); A } at //Java is a double-byte encoding, UTF-16BE encoding - //where Chinese occupies two bytes, English occupies two bytes - System.out.println (); - byte[] Byte4=s.getbytes ("Utf-16be"); - for(byteb:byte4) { -System.out.print (integer.tohexstring (B&0XFF) + ""); in } - System.out.println (); toString st1=NewString (Byte4, "Utf-16be"); + System.out.print (ST1); - the System.out.println (); * byte[] Byte5=s.getbytes ("Us-ascii"); $ for(byteb:byte5) {Panax NotoginsengSystem.out.print (integer.tohexstring (B&0XFF) + ""); - } the } + A}
Conversion method between binaries in Java