Android字符串和十六进制相互转化出现的中文乱码问题
程序员文章站
2024-02-16 10:30:28
废话不读说了,直接给大家贴代码了,代码附有注释,可以说明一切,本文写的不好,还请见谅。
import java.io.bytearrayoutputstream;...
废话不读说了,直接给大家贴代码了,代码附有注释,可以说明一切,本文写的不好,还请见谅。
import java.io.bytearrayoutputstream; /** * created by administrator on 2016/2/2. * -----------16进制和字符串互转--------- * ------------解决中文乱码问题--------- */ public class stringtosixthutils { private static string hexstring = "0123456789abcdef"; /* * 将字符串编码成16进制数字,适用于所有字符(包括中文) */ public static string encode(string str) { //根据默认编码获取字节数组 byte[] bytes = str.getbytes(); stringbuilder sb = new stringbuilder(bytes.length * 2); //将字节数组中每个字节拆解成2位16进制整数 for (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(); } /* * 将16进制数字解码成字符串,适用于所有字符(包括中文) */ public static string decode(string bytes) { bytearrayoutputstream baos = new bytearrayoutputstream(bytes.length() / 2); //将每2位16进制整数组装成一个字节 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进制字符串和字节数组互相转换
package com.wpn.net.util; public class numberchange { /* * 把16进制字符串转换成字节数组 @param hex @return */ public static byte[] hexstringtobyte(string hex) { int len = (hex.length() / 2); byte[] result = new byte[len]; char[] achar = hex.tochararray(); 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; } /** * 把字节数组转换成16进制字符串 * * @param barray * @return */ public static final string bytestohexstring(byte[] barray) { stringbuffer sb = new stringbuffer(barray.length); string stemp; for (int i = 0; i < barray.length; i++) { stemp = integer.tohexstring(0xff & barray[i]); if (stemp.length() < 2) sb.append(0); sb.append(stemp.touppercase()); } return sb.tostring(); } /** * @函数功能: bcd码转为10进制串(阿拉伯数据) * @输入参数: bcd码 * @输出结果: 10进制串 */ public static string bcd2str(byte[] bytes) { stringbuffer temp = new stringbuffer(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { temp.append((byte) ((bytes[i] & 0xf0) >>> 4)); temp.append((byte) (bytes[i] & 0x0f)); } return temp.tostring().substring(0, 1).equalsignorecase("0") ? temp.tostring().substring(1) : temp.tostring(); } /** * @函数功能: 10进制串转为bcd码 * @输入参数: 10进制串 * @输出结果: bcd码 */ public static byte[] str2bcd(string asc) { int len = asc.length(); int mod = len % 2; if (mod != 0) { asc = "0" + asc; len = asc.length(); } byte abt[] = new byte[len]; if (len >= 2) { len = len / 2; } byte bbt[] = new byte[len]; abt = asc.getbytes(); int j, k; for (int p = 0; p < asc.length() / 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[str.length]; for(int i=0;i<str.length;i++){ b[i] = hexstringtobyte(str[i])[0]; } system.out.println(new string(b)); string strc ="你好"; string bth=bytestohexstring(strc.getbytes()); system.out.println(bth); system.out.println(short.max_value); system.out.println(integer.tobinarystring(280)); } }
以上所述是本文给大家分享的android字符串和十六进制相互转化出现的中文乱码问题的相关内容,希望对大家有所帮助。