Android-conversion between string and hexadecimal (solving Chinese garbled characters) and android-hexadecimal
Import java. io. byteArrayOutputStream;/*** Created by Administrator on 2016/2/2. * ----------- hexadecimal and String Conversion --------- * ------------ solve Chinese garbled characters --------- */public class StringToSixthUtils {private static String hexString = "0123456789 abcdef "; /** encode the String into a hexadecimal number, applicable to all characters (including Chinese) */public static String encode (String str) {// obtain byte array byte [] bytes = str according to the default encoding. getBytes (); StringBuilder sb = new StringBuilder (bytes. length * 2); // splits each byte in the byte array into two hexadecimal integers (int I = 0; I <bytes. length; I ++) {sb. append (hexString. charAt (bytes [I] & 0xf0)> 4); sb. append (hexString. charAt (bytes [I] & 0x0f);} return sb. toString ();}/** decodes a hexadecimal number into a String, applicable to all characters (including Chinese) */public static String decode (String bytes) {ByteArrayOutputStream baos = new ByteArrayOutputStream (bytes. length ()/2); // Assemble every two hexadecimal 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 ());}}