欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

AIX由于字符集造成getBytes数据与linux不一致问题

程序员文章站 2022-03-10 08:23:48
...
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.nio.charset.Charset;

import javax.xml.bind.DatatypeConverter;

public class GetUnicodeTest {
	
	public static void main(String[] args) throws Exception {
		String hex1= DatatypeConverter.printHexBinary("12".getBytes());
		System.out.println("getBytes:" + hex1);
		
		String hex2= DatatypeConverter.printHexBinary("12".getBytes("UTF-8"));
		System.out.println("getBytes(UTF-8):" + hex2);
		
		String hex3= DatatypeConverter.printHexBinary("12你好".getBytes());
		System.out.println("getBytes:" + hex3);
		
		String hex4= DatatypeConverter.printHexBinary("12你好".getBytes("UTF-16"));
		System.out.println("getBytes(UTF-16):" + hex4);
		
		String hex5= DatatypeConverter.printHexBinary("12你好".getBytes("UnicodeBigUnmarked"));
		System.out.println("getBytes(UnicodeBigUnmarked):" + hex5);
		
		
	}
	
}



#/opt/IBM/db2/V10.5/java/jdk64/bin/javac GetUnicodeTest.java 
#/opt/IBM/db2/V10.5/java/jdk64/bin/java GetUnicodeTest
getBytes:3132
getBytes(UTF-8):3132
getBytes:3132E4BDA0E5A5BD
getBytes(UTF-16):FEFF0031003200E400BD00A000E500A500BD
getBytes(UnicodeBigUnmarked):0031003200E400BD00A000E500A500BD


export LANG=ZH_CN.UTF-8
#/opt/IBM/db2/V10.5/java/jdk64/bin/javac GetUnicodeTest.java 
#/opt/IBM/db2/V10.5/java/jdk64/bin/java GetUnicodeTest
getBytes:3132
getBytes(UTF-8):3132
getBytes:3132E4BDA0E5A5BD
getBytes(UTF-16):FEFF003100324F60597D
getBytes(UnicodeBigUnmarked):003100324F60597D

安装UTF-8字符集,并设置环境变量export LANG=ZH_CN.UTF-8后重编demo,转换正常。

相关标签: aix java aix