Created
October 12, 2017 04:47
-
-
Save nobodxbodon/f01a6853ff236703ed2751bd2cf20848 to your computer and use it in GitHub Desktop.
为验证偶数个字符在UTF8<->GBK互转时也会出现乱码 https://github.com/program-in-chinese/overview/issues/26#issuecomment-336012338
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.UnsupportedEncodingException; | |
public class UTF8_GBK { | |
public static void main(String[] args) throws UnsupportedEncodingException { | |
String 初始字符串 = "开始"; | |
byte[] 字节 = 初始字符串.getBytes("utf-8"); | |
System.out.println("转码前:" + encodeHex(字节)); | |
String 转编码后 = new String(字节, "GBK"); | |
byte[] 转码字节 = 转编码后.getBytes("GBK"); | |
System.out.println("转码后:" + encodeHex(转码字节)); | |
String 二转编码后 = new String(转码字节, "utf-8"); | |
System.out.println(二转编码后); | |
} | |
public static final String encodeHex(byte[] bytes) { | |
StringBuffer buff = new StringBuffer(bytes.length * 2); | |
String b; | |
for (int i = 0; i < bytes.length; i++) { | |
b = Integer.toHexString(bytes[i]); | |
// byte是两个字节的,而上面的Integer.toHexString会把字节扩展为4个字节 | |
buff.append(b.length() > 2 ? b.substring(6, 8) : b); | |
buff.append(" "); | |
} | |
return buff.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment