I will not read nonsense anymore, so I will post code to you directly. The code is accompanied by comments, which can explain everything. I am sorry for the poor writing of this article.
import ; /** * Created by Administrator on 2016/2/2. * --------------------------- * ------------------------------------------------------------------------------------------------------------------------------- */ public class StringToSixthUtils { private static String hexString = "0123456789abcdef"; /* * Encode the string into hexadecimal digits, suitable for all characters (including Chinese) */ public static String encode(String str) { //Get byte array according to the default encodingbyte[] bytes = (); StringBuilder sb = new StringBuilder( * 2); //Disassemble each byte in the byte array into a 2-bit hexadecimal integerfor (int i = 0; i < ; i++) { (((bytes[i] & 0xf0) >> 4)); (((bytes[i] & 0x0f))); } return (); } /* * Decode hexadecimal numbers into strings, suitable for all characters (including Chinese) */ public static String decode(String bytes) { ByteArrayOutputStream baos = new ByteArrayOutputStream(() / 2); //Assemble each 2-bit hexadecimal integer into one bytefor (int i = 0; i < (); i += 2) ((((i)) << 4 | ((i + 1)))); return new String(()); } }
Below I share a piece of code with you about converting hexadecimal strings and byte arrays into each other
package ; public class NumberChange { /* * Convert hex string into byte array @param hex @return */ public static byte[] hexStringToByte(String hex) { int len = (() / 2); byte[] result = new byte[len]; char[] achar = (); for (int i = 0; i < len; i++) { int pos = i * 2; result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1])); } return result; } private static byte toByte(char c) { byte b = (byte) "0123456789ABCDEF".indexOf(c); return b; } /** * Convert byte array into hexadecimal string * * @param bArray * @return */ public static final String bytesToHexString(byte[] bArray) { StringBuffer sb = new StringBuffer(); String sTemp; for (int i = 0; i < ; i++) { sTemp = (0xFF & bArray[i]); if (() < 2) (0); (()); } return (); } /** * @function function: convert BCD code into decimal string (Arabic data) * @Input parameters: BCD code * @Output result: Decimal string */ public static String bcd2Str(byte[] bytes) { StringBuffer temp = new StringBuffer( * 2); for (int i = 0; i < ; i++) { ((byte) ((bytes[i] & 0xf0) >>> 4)); ((byte) (bytes[i] & 0x0f)); } return ().substring(0, 1).equalsIgnoreCase("0") ? ().substring(1) : (); } /** * @function function: convert decimal string to BCD code * @Input parameters: decimal string * @Output result: BCD code */ public static byte[] str2Bcd(String asc) { int len = (); int mod = len % 2; if (mod != 0) { asc = "0" + asc; len = (); } byte abt[] = new byte[len]; if (len >= 2) { len = len / 2; } byte bbt[] = new byte[len]; abt = (); int j, k; for (int p = 0; p < () / 2; p++) { if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) { j = abt[2 * p] - '0'; } else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) { j = abt[2 * p] - 'a' + 0x0a; } else { j = abt[2 * p] - 'A' + 0x0a; } if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) { k = abt[2 * p + 1] - '0'; } else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) { k = abt[2 * p + 1] - 'a' + 0x0a; } else { k = abt[2 * p + 1] - 'A' + 0x0a; } int a = (j << 4) + k; byte b = (byte) a; bbt[p] = b; } return bbt; } public static void main(String[] arg) { /** * 68 65 6C 6C 6 F 0A * C4 E3 BA C3 */ String[] str = {"C4", "E3", "BA", "C3"}; // String[] str = {"7E","02","04","00","07","10","00","00","00","00","13","08","4F","01","0B","0B","15","10","14","13","44","7E"}; byte[] b = new byte[]; for(int i=0;i<;i++){ b[i] = hexStringToByte(str[i])[0]; } (new String(b)); String strC ="Hello"; String bth=bytesToHexString(()); (bth); (Short.MAX_VALUE); ((280)); } }
The above is the relevant content of the Chinese garbled problem that Android strings and hexadecimal converts each other that this article shares with you. I hope it will be helpful to you.