这个其实不算陵迹太难。
hex有格式的,根尺行并据格式,转成byte[],再写入文件就是了。最重要的工作就是16进带哪制字符转两位byte[]。
JAVA 读进来,一丛或唯样渗培相加团空int n=0xff&(0x01 +~(0x02 + 00 + 00 + 0x02 + 0x12 + 00))
String a=Integer.toHexString(n)
System.out.println(a)
private static String hexString = "0123456789ABCDEF"public static void main(String[] args) {
System.out.println(encode("中文"))
System.out.println(decode(encode("中文李洞")))
}
/*
* 将字符串编码成16进制数字,适用于所有字符(包括中文)
*/
public static String encode(String str) {
// 根据默认编码获取<a href="https://www.baidu.com/s?wd=%E5%AD%97%E8%8A%82%E6%95%B0%E7%BB%84&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Ydn1D4nWDvuWN9mvRvnWDv0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHT4rjR3n101njc4PHnLrHTYPs" target="_blank" class="baidu-highlight">字节数组</a>
byte[] bytes = str.getBytes()
StringBuilder sb = new StringBuilder(bytes.length * 2)
// 将<a href="https://www.baidu.com/s?wd=%E5%AD%97%E8%8A%82%E6%95%B0%E7%BB%84&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Ydn1D4nWDvuWN9mvRvnWDv0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHT4rjR3n101njc4PHnLrHTYPs" target="_blank" class="baidu-highlight">字节数组</a>中每个字节拆解成2位16进制整数
for (int i = 0i <bytes.lengthi++) {
sb.append(hexString.charAt((bytes[i] &0xf0) >>4))
sb.append(hexString.charAt((bytes[i] &0x0f) >哪档枯>0))
}
return sb.toString()
}
/*
* 将16进制数字解码成字符串,适用于所有字符(包括中文)
*/
public static String decode(String bytes) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length() / 2)
/蠢蔽/ 将每2位16进制整数组装成一个字节
for (int i = 0i <bytes.length()i += 2)
baos.write((hexString.indexOf(bytes.charAt(i)) <<4 | hexString
.indexOf(bytes.charAt(i + 1))))
return new String(baos.toByteArray())
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)