⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 encoding.java

📁 采用JAVA开发
💻 JAVA
字号:
/*
 * Created on 2004-6-28
 * Created by Administrator
 */
package com.gctech.sms.sdsms.common;

import sun.io.ByteToCharConverter;
import sun.io.CharToByteConverter;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 * <p>Copyright: GCTech (c) 2004-6-28</p>
 * <p>Company: 国创科技</p>
 * <p>Email: ly@gctech.com.cn</p>
 *
 * @version 1.0
 * @author liyi
 *
 */
public class Encoding {

	public static final String encoding1 = "gb2312";
	public static final String encoding2 = "ISO-8859-1";
	public static void main(String[] args) throws Exception {
		System.out.println(new String(toUtf8String("test中国").getBytes()));
//		String s = "你";
//		System.out.println("UNICODE:"+s.charAt(0));
//		byte[] b = s.getBytes("iso8859-1");
//		for (int i = 0; i < b.length; i++) {
//			System.out.println(Integer.toHexString(b[i]));
//		}
//		System.out.println(new String(b,"gb2312"));
		//charTobye();
		//byteToChar();
	}
	public static void charTobye() throws Exception {
		char[] c = { '\u4F60' };
		//String ni = "你";
		CharToByteConverter convertor =
			CharToByteConverter.getConverter(encoding2);
		byte[] b1 = convertor.convertAll(c);
		for (int i = 0; i < b1.length; i++) {
			System.out.println(Integer.toHexString(b1[i]));
		}
		System.out.println(new String(b1, encoding1));
	}
	public static void byteToChar() throws Exception {
		byte b[] = {(byte) '\u00c4', (byte) '\u00E3' };
		ByteToCharConverter convertor =
			ByteToCharConverter.getConverter(encoding1);
		char[] c = convertor.convertAll(b);
		for (int i = 0; i < c.length; i++) {
			System.out.println(Integer.toHexString(c[i]));
		}
		System.out.println(new String(c));
	}
	public static String toUtf8String(String s) {
		StringBuffer sb = new StringBuffer();
		for (int i=0;i<s.length();i++){
			 char c = s.charAt(i);
			 if (c >= 0 && c <= 255) {
			   sb.append(c);
			 } else {
			   byte[] b;
			   try {
				 b = Character.toString(c).getBytes("utf-8");
			   } catch (Exception ex) {
				 System.out.println(ex);
				 b = new byte[0];
			   }
			   for (int j = 0; j < b.length; j++) {
				 int k = b[j];
				 if (k < 0) 
				 	k += 256;
				 	sb.append(k);
			   }
			 }
		  }
		  return sb.toString();
	  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -