一个乱码的问题

今天上班,升级脚本时候报错,网管服务端显示出错的sql有乱码。对上传到服务端的sql文件,用vi打开没问题,但是用more打开则显示乱码 怀疑是编码问题。UE打开出错sql文件,ctrl+H显示前导字符为“EFBBBF”,该文件为UTF-8编码。网管服务端的乱码为“锘縰pdate...”是否网管服务端将其当做GBK进行转码呢?写了一小段程序验证了我的猜想

 

	public static String bin2hex(String bin, String charSet) throws Exception {
		char[] digital = "0123456789ABCDEF".toCharArray();
		StringBuffer sb = new StringBuffer("");
		byte[] bs = bin.getBytes(charSet);
		int bit;
		for (int i = 0; i < bs.length; i++) {
			bit = (bs[i] & 0x0f0) >> 4;
			sb.append(digital[bit]);
			bit = bs[i] & 0x0f;
			sb.append(digital[bit]);
		}
		return sb.toString();
	}

	public static String hex2bin(String hex, String charSet) throws Exception {
		String digital = "0123456789ABCDEF";
		char[] hex2char = hex.toCharArray();
		byte[] bytes = new byte[hex.length() / 2];
		int temp;
		for (int i = 0; i < bytes.length; i++) {
			temp = digital.indexOf(hex2char[2 * i]) * 16;
			temp += digital.indexOf(hex2char[2 * i + 1]);
			bytes[i] = (byte) (temp & 0xff);
		}
		return new String(bytes, charSet);
	}

	public static void main(String[] args) throws Exception {

		String str = "update";
		// 757064617465
		System.out.println(bin2hex(str, "UTF-8"));
		// 757064617465
		System.out.println(bin2hex(str, "GBK"));
		// EFBBBF是UTF-8前缀
		String ss = "EFBBBF" + bin2hex(str, "UTF-8");
        //锘縰pdate
		System.out.println(hex2bin(ss, "GBK"));
	}

 

你可能感兴趣的:(乱码)