简体与繁体字符串之间的转换

本文介绍了一个用于实现GB2312与Big5编码相互转换的Java工具类。该工具通过加载预设的对照表来实现两种编码间的准确转换,并提供了实例演示。
package com.newbee.demo;



/* GB2Big5 - Decompiled by JODE

 * Visit http://jode.sourceforge.net/

 */

import java.io.BufferedOutputStream;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.util.Properties;



public class GB2Big5 {

	private static GB2Big5 pInstance = null;



	private String s_big5TableFile = null;



	private String s_gbTableFile = null;



	private byte[] b_big5Table = null;



	private byte[] b_gbTable = null;



	private GB2Big5(String sgbTableFile, String sbig5TableFile)

			throws NullPointerException {

		s_big5TableFile = sbig5TableFile;

		s_gbTableFile = sgbTableFile;

		if (null == b_gbTable)

			b_gbTable = getBytesFromFile(sgbTableFile);

		if (null == b_big5Table)

			b_big5Table = getBytesFromFile(sbig5TableFile);

		if (null == b_gbTable)

			throw new NullPointerException("No gb table can be load");

		if (null == b_big5Table)

			throw new NullPointerException("No big5 table can be load");

	}

	

	// 字符转换对照表

	public static synchronized GB2Big5 getInstance() {

		return getInstance("/com/newbee/demo/gb-big5.table",

				"/com/newbee/demo/big5-gb.table");

	}



	private static synchronized GB2Big5 getInstance(String sgbTableFile,

			String sbig5TableFile) {

		if (null == pInstance) {

			try {

				pInstance = new GB2Big5(sgbTableFile, sbig5TableFile);

			} catch (Exception e) {

				System.err.println(e.toString());

				pInstance = null;

			}

		}

		return pInstance;

	}



	protected synchronized void resetBig5Char(String gbChar, String big5Char)

			throws Exception {

		byte[] Text = new String(gbChar.getBytes(), System.getProperty("file.encoding")).getBytes("GBK");

		byte[] TextBig5 = new String(big5Char.getBytes(), System.getProperty("file.encoding"))

				.getBytes("BIG5");

		int max = Text.length - 1;

		int h = 0;

		int l = 0;

		int p = 0;

		int b = 256;

		byte[] big = new byte[2];

		for (int i = 0; i < max; i++) {

			h = Text[i];

			if (h < 0) {

				h = b + h;

				l = Text[i + 1];

				if (l < 0)

					l = b + Text[i + 1];

				if (h != 161 || l != 64) {

					p = (h - 160) * 510 + (l - 1) * 2;

					b_gbTable[p] = TextBig5[i];

					b_gbTable[p + 1] = TextBig5[i + 1];

				}

				i++;

			}

		}

		BufferedOutputStream pWriter = new BufferedOutputStream(

				new FileOutputStream(s_gbTableFile));

		pWriter.write(b_gbTable, 0, b_gbTable.length);

		pWriter.close();

	}



	protected synchronized void resetGbChar(String big5Char, String gbChar)

			throws Exception {

		byte[] TextGb = new String(gbChar.getBytes(), System.getProperty("file.encoding")).getBytes("GBK");

		byte[] Text = new String(big5Char.getBytes(), System.getProperty("file.encoding")).getBytes("BIG5");

		int max = Text.length - 1;

		int h = 0;

		int l = 0;

		int p = 0;

		int b = 256;

		byte[] big = new byte[2];

		for (int i = 0; i < max; i++) {

			h = Text[i];

			if (h < 0) {

				h = b + h;

				l = Text[i + 1];

				if (l < 0)

					l = b + Text[i + 1];

				if (h != 161 || l != 64) {

					p = (h - 160) * 510 + (l - 1) * 2;

					b_big5Table[p] = TextGb[i];

					b_big5Table[p + 1] = TextGb[i + 1];

				}

				i++;

			}

		}

		BufferedOutputStream pWriter = new BufferedOutputStream(

				new FileOutputStream(s_big5TableFile));

		pWriter.write(b_big5Table, 0, b_big5Table.length);

		pWriter.close();

	}



	public byte[] gb2big5(String inStr) throws Exception {

		if (null == inStr || inStr.length() <= 0)

			return "".getBytes();

		byte[] Text = new String(inStr.getBytes(), System.getProperty("file.encoding")).getBytes("GBK");

		int max = Text.length - 1;

		int h = 0;

		int l = 0;

		int p = 0;

		int b = 256;

		byte[] big = new byte[2];

		for (int i = 0; i < max; i++) {

			h = Text[i];

			if (h < 0) {

				h = b + h;

				l = Text[i + 1];

				if (l < 0)

					l = b + Text[i + 1];

				if (h == 161 && l == 64)

					big[0] = big[1] = (byte) (161 - b);

				else {

					p = (h - 160) * 510 + (l - 1) * 2;

					try {

						big[0] = (byte) (b_gbTable[p] - b);

					} catch (Exception e) {

						big[0] = (byte) 45;

					}

					try {

						big[1] = (byte) (b_gbTable[p + 1] - b);

					} catch (Exception e) {

						big[1] = (byte) 45;

					}

				}

				Text[i] = big[0];

				Text[i + 1] = big[1];

				i++;

			}

		}

		return Text;

	}



	public byte[] big52gb(String inStr) throws Exception {

		if (null == inStr || inStr.length() <= 0)

			return new byte[0];

		byte[] Text = new String(inStr.getBytes(), System.getProperty("file.encoding")).getBytes("BIG5");

		int max = Text.length - 1;

		int h = 0;

		int l = 0;

		int p = 0;

		int b = 256;

		byte[] big = new byte[2];

		for (int i = 0; i < max; i++) {

			h = Text[i];

			if (h < 0) {

				h = b + h;

				l = Text[i + 1];

				if (l < 0)

					l = b + Text[i + 1];

				if (h == 161 && l == 161) {

					big[0] = (byte) (161 - b);

					big[1] = (byte) (64 - b);

				} else {

					p = (h - 160) * 510 + (l - 1) * 2;

					try {

						big[0] = (byte) (b_big5Table[p] - b);

					} catch (Exception e) {

						big[0] = (byte) 45;

					}

					try {

						big[1] = (byte) (b_big5Table[p + 1] - b);

					} catch (Exception e) {

						big[1] = (byte) 45;

					}

				}

				Text[i] = big[0];

				Text[i + 1] = big[1];

				i++;

			}

		}

		return Text;

	}



	private static byte[] getBytesFromFile(String inFileName) {

		byte[] is;

		try {

			InputStream in = GB2Big5.class.getResourceAsStream(inFileName);

			ByteArrayOutputStream out = new ByteArrayOutputStream(1024);

			byte[] bt = new byte[1024];

			for (int i = 0; (i = in.read(bt)) != -1;) {

				out.write(bt, 0, i);

			}

			in.close();

			is = out.toByteArray();

		} catch (Exception e) {

			e.printStackTrace();

			return null;

		}

		return is;

	}



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

		GB2Big5 pTmp = GB2Big5.getInstance(); 

		System.out.println("我是繁體字=>"+new String(pTmp.big52gb("我是繁體字"),"GBK"));

		System.out.println("我是简体字=>"+new String(pTmp.gb2big5("我是简体字"),"BIG5"));

	}



}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值