ImportJava.io.ByteArrayOutputStream;/*** Created by Administrator on 2016/2/2. *-----------16 binary and string mutual transfer---------*------------solve the problem of Chinese garbled---------*/ Public classStringtosixthutils {Private StaticString hexstring = "0123456789abcdef"; /** Encode a string into 16 digits for all characters (including Chinese)*/ Public Staticstring Encode (String str) {//get byte array by default encoding byte[] bytes =str.getbytes (); StringBuilder SB=NewStringBuilder (Bytes.length * 2); //splits each byte in a byte array into a 2-bit 16-decimal integer for(inti = 0; i < bytes.length; i++) {sb.append (Hexstring.charat (bytes[i)& 0xf0) >> 4)); Sb.append (Hexstring.charat (bytes[i)& 0x0f))); } returnsb.tostring (); } /** Decodes 16 binary digits into a string for all characters (including Chinese)*/ Public Staticstring Decode (string bytes) {Bytearrayoutputstream BAOs=NewBytearrayoutputstream (Bytes.length ()/2); //assemble every 2 bits of 16 integers into one byte for(inti = 0; I < bytes.length (); i + = 2) Baos.write ((Hexstring.indexof (Bytes.charat (i) )<< 4 | Hexstring.indexof (Bytes.charat (i + 1)))); return NewString (Baos.tobytearray ()); }}
android--string and 16 binary conversion (solve Chinese garbled)