`
wingware
  • 浏览: 142015 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

java unicode to utf-8

阅读更多
package test.wingware;

public class UnicodeToUTF8 {

	private static final int MASKBITS = 0x3F;
	private static final int MASKBYTE = 0x80;
	private static final int MASK2BYTES = 0xC0;
	private static final int MASK3BYTES = 0xE0;

	// private static final int MASK4BYTES = 0xF0;
	// private static final int MASK5BYTES = 0xF8;
	// private static final int MASK6BYTES = 0xFC;
	/** */
	/**
	 * @功能: 将UNICODE(UTF-16LE)编码转成UTF-8编码
	 * @参数: byte[] b 源字节数组
	 * @返回值: byte[] b 转为UTF-8编码后的数组
	 * @作者: imuse
	 */
	public static byte[] UNICODE_TO_UTF8(byte[] b) {
		int i = 0;
		int j = 0;
		byte[] utf8Byte = new byte[b.length * 2];
		while (i < b.length) {
			byte[] bUTF = new byte[1];
			int nCode = (b[i] & 0xFF) | ((b[i + 1] & 0xFF) << 8);
			if (nCode < 0x80) {
				bUTF = new byte[1];
				bUTF[0] = (byte) nCode;
			}
			// 110xxxxx 10xxxxxx
			else if (nCode < 0x800) {
				bUTF = new byte[2];
				bUTF[0] = (byte) (MASK2BYTES | nCode >> 6);
				bUTF[1] = (byte) (MASKBYTE | nCode & MASKBITS);
			}
			// 1110xxxx 10xxxxxx 10xxxxxx
			else if (nCode < 0x10000) {
				bUTF = new byte[3];
				bUTF[0] = (byte) (MASK3BYTES | nCode >> 12);
				bUTF[1] = (byte) (MASKBYTE | nCode >> 6 & MASKBITS);
				bUTF[2] = (byte) (MASKBYTE | nCode & MASKBITS);
			}
			for (int k = 0; k < bUTF.length; k++) {
				utf8Byte[j++] = bUTF[k];
			}
			i += 2;
		}
		b = new byte[j];
		System.arraycopy(utf8Byte, 0, b, 0, j);
		return b;
	}

	/** */
	/**
	 * @功能: 将一个长度为2 byte数组转为short
	 * @参数: byte[] bytesShort要转的字节数组
	 * @返回值: short sRet 转后的short值
	 */
	public static short bytesToShort(byte[] bytesShort) {
		short sRet = 0;
		sRet += (bytesShort[0] & 0xFF) << 8;
		sRet += bytesShort[1] & 0xFF;
		return sRet;
	}

	/** */
	/**
	 * @功能: 将一个short值转为byte数组
	 * @参数: short sNum 要转的short值
	 * @返回值: byte[] bytesRet 转后的byte数组
	 */
	public static byte[] shortToBytes(short sNum) {
		byte[] bytesRet = new byte[2];
		bytesRet[0] = (byte) ((sNum >> 8) & 0xFF);
		bytesRet[1] = (byte) (sNum & 0xFF);
		return bytesRet;
	}

	/** */
	/**
	 * @功能: 将一个长度为4 byte数组转为int
	 * @参数: byte[] bNum要转的字节数组
	 * @返回值: int retInt 转后的int值
	 */
	public static int bytesToInt(byte[] bNum) {
		int retInt = 0;
		retInt = ((bNum[0] & 0xFF) << 24);
		retInt += (bNum[1] & 0xFF) << 16;
		retInt += (bNum[2] & 0xFF) << 8;
		retInt += bNum[3] & 0xFF;
		return retInt;
	}

	/** */
	/**
	 * @功能: 将一个int值转为byte数组
	 * @参数: int nNum 要转的int值
	 * @返回值: byte[] bytesRet 转后的byte数组
	 */
	public static byte[] intToBytes(int nNum) {
		byte[] bytesRet = new byte[4];
		bytesRet[0] = (byte) ((nNum >> 24) & 0xFF);
		bytesRet[1] = (byte) ((nNum >> 16) & 0xFF);
		bytesRet[2] = (byte) ((nNum >> 8) & 0xFF);
		bytesRet[3] = (byte) (nNum & 0xFF);
		return bytesRet;
	}

}
分享到:
评论
2 楼 ap0406708 2012-08-08  
ap0406708 写道
有个bug

Sorry,我错了。完美无bug~~~thx your code.
1 楼 ap0406708 2012-08-08  
有个bug

相关推荐

    MADEDIT 多标签

    Unicode(UTF-8, UTF-16/32 with Little or Big Endian), Big5, GBK and S-JIS etc. * Supports Unicode CJK Ext-B. * If users input a character that is not supported by current encoding, this character will...

    wordlist-generator

    Unicode / UTF-8支持 以组合和排列方式生成单词表 支持指定特定长度 支持数字和符号 分别支持大写和小写字符 删除重复的字符 生成特定网站或网页的单词表 更改临时文件和单词表的默认输出位置 单词表完成后发送电子...

    conv_gbk_ibm1388:GBKIBM1388字符集转换

    这两个字符集不能通过像Unicode在UTF-8和UTF-16之间所做的操作来直接互换。 它们基于具有不同汉字顺序的不同编码(ASCII / EBCDIC)。 ICU( )支持两种字符集,但占用空间很大。 这是一种占地面积小,快速的解决...

    json:Ruby的JSON实现

    Ruby的JSON实现 描述 这是根据RFC 7159 的JSON规范的实现。 从版本1.0.0开始,将提供两个变体: ... 要对未经UTF-8编码的原始二进制字符串进行编码,请使用String的to_json_raw_object方法(该方法生成一个

    新版Android开发教程.rar

    Note: Note: Note: Note: If JDK is already installed on your development computer, please take a moment to make sure that it meets the version requirements listed above. In particular, note that some ...

    CE中文版-启点CE过NP中文.exe

    Debugging: Added an option to chose if you wish to break on unexpected breakpoints, and if CE should break on unexpected breakpoints, or only on specified regions (like AA scripts) Disassembler: The ...

    java8看不到源码-amanda:一个用java8、springboot、jpa、hibernate-search和reactjs编写的博客

    'utf8_unicode_ci'; USE amanda; grant all privileges on *.* to 'amanda'@'localhost' identified by '123456'; grant all privileges on *.* to 'amanda'@'127.0.0.1' identified by '123456'; grant all ...

    CJK-decomposition:存储库可用于将中文,日文(日文汉字)或韩文(汉字)字符分解为较小的部分

    目标是将每个字符分解成较小的部分,而我们仅允许分解为实际unicode的一部分,并且可以通过UTF-8编码存储 作为分解基础,我们使用“ ids.txt”,它是的副本。 建造 git clone ...

    XML轻松学习手册--XML肯定是未来的发展趋势,不论是网页设计师还是网络程序员,都应该及时学习和了解

    面向对象的思想方法已经非常流行了,在编程语言(例如java,js)中,都运用面向对象的编程思想。在XML中,就是要将网页也作为一个对象来操作和控制,我们可以建立自己的对象和模板。与对象进行交流,如何命令对象,...

    JavaScript笔记

    强调:HTML、CSS、JS都要使用UTF-8编码保存(window系统) 使用js文件引入网页:[removed][removed] 强调:一旦定义src属性则其中的代码失效 解释执行:语句也可以直接写在js文件中,边解释边执行 3.***调试*** ...

    python3.6.5参考手册 chm

    PEP 529: Change Windows filesystem encoding to UTF-8 PEP 528: Change Windows console encoding to UTF-8 PEP 520: Preserving Class Attribute Definition Order PEP 468: Preserving Keyword Argument ...

    所有常用中英文ttf字体包,包含几个手写字体

    1. Uncompress the archive # tar xvzf utf8.tar.gz or # tar xvjf arial.tar.bz2 2. Create a directory for new fonts # mkdir /usr/share/fonts/truetype 3. Move the uncompressed font files to the new font ...

    MySQL5.1参考手册官方简体中文版

    10.6. 用于元数据的UTF8 10.7. 与其它DBMS的兼容性 10.8. 新字符集配置文件格式 10.9. 国家特有字符集 10.10. MySQL支持的字符集和校对 10.10.1. Unicode字符集 10.10.2. 西欧字符集 10.10.3. 中欧字符集 10.10.4. ...

    MySQL 5.1参考手册

    10.6. 用于元数据的UTF8 10.7. 与其它DBMS的兼容性 10.8. 新字符集配置文件格式 10.9. 国家特有字符集 10.10. MySQL支持的字符集和校对 10.10.1. Unicode字符集 10.10.2. 西欧字符集 10.10.3. 中欧字符集 10.10.4. ...

    MySQL 5.1官方简体中文参考手册

    10.6. 用于元数据的UTF8 10.7. 与其它DBMS的兼容性 10.8. 新字符集配置文件格式 http://doc.mysql.cn/mysql5/refman-5.1-zh.html-chapter/(第 9/24 页)2006-11-02 19:12:13 MySQL 5.1 Reference Manual 10.9. ...

    mysql5.1中文手册

    用于元数据的UTF8 10.7. 与其它DBMS的兼容性 10.8. 新字符集配置文件格式 10.9. 国家特有字符集 10.10. MySQL支持的字符集和校对 10.10.1. Unicode字符集 10.10.2. 西欧字符集 10.10.3. 中欧...

    mysql官方中文参考手册

    10.6. 用于元数据的UTF8 10.7. 与其它DBMS的兼容性 10.8. 新字符集配置文件格式 10.9. 国家特有字符集 10.10. MySQL支持的字符集和校对 10.10.1. Unicode字符集 10.10.2. 西欧字符集 10.10.3. 中欧字符集 10.10.4. ...

    MYSQL中文手册

    10.6. 用于元数据的UTF8 10.7. 与其它DBMS的兼容性 10.8. 新字符集配置文件格式 10.9. 国家特有字符集 10.10. MySQL支持的字符集和校对 10.10.1. Unicode字符集 10.10.2. 西欧字符集 10.10.3. 中欧字符集 ...

Global site tag (gtag.js) - Google Analytics