Package XXXX;
Import Java.io.ByteArrayOutputStream;
/**
* Description string (contains Chinese) and conversion between 16 binary
*/
public class Codechange {
/**
* 16 binary numeric Character set
*/
private static String hexstring= "0123456789ABCDEF";
 
public static void Main (string[] args) {
system.out.println (Tostringhex ("41"));
}
/**
* Conversion string hexadecimal encoding
*/
public static string tohexstring (string s) {
string str= "";
for (int i=0;i<s.length (); i++) {
int ch = (int) s.charat (i);
string s4 = Integer.tohexstring (ch);
str = str + s4;
}
return str;
}
/**
* Convert hexadecimal encoding to string
*/
public static string Tostringhex (String s) {
byte[] Bakeyword = new Byte[s.length ()/2];
for (int i = 0; i < bakeyword.length; i++) {
try{
Bakeyword[i] = (byte) (0xFF & Integer.parseint (s.substring (i*2, i*2+2), 16));
}catch (Exception e) {
e.printstacktrace ();
}
}
try{
s = new String (Bakeyword, "Utf-8");//utf-16le:not
}catch (Exception E1) {
E1.printstacktrace ();
}
return s;
}
/**
* Encode a string into 16 digits for all characters (including Chinese)
*/
public static string encode (String str) {
Get byte array by default encoding
Byte[] Bytes=str.getbytes ();
StringBuilder sb=new StringBuilder (bytes.length*2);
Splits each byte in a byte array into a 2-bit 16-decimal integer
for (int i=0;i<bytes.length;i++) {
Sb.append (Hexstring.charat ((bytes[i]&0xf0) >>4));
Sb.append (Hexstring.charat ((bytes[i]&0x0f) >>0));
}
return sb.tostring ();
}
/**
* Decodes 16 binary digits into a string for all characters (including Chinese)
*/
public static string decode (string bytes)
{
Bytearrayoutputstream baos=new Bytearrayoutputstream (Bytes.length ()/2);
Assemble every 2 bits of 16 integers into one byte
for (int i=0;i<bytes.length (); i+=2)
Baos.write ((Hexstring.indexof (Bytes.charat (i)) <<4 |hexstring.indexof (Bytes.charat (i+1)));
return new String (Baos.tobytearray ());
}
}
16 Conversion of binary and string (including Chinese)