字符串转化为unicode编码
2009-07-31 16:16
495 查看
package com.util;
import java.io.UnsupportedEncodingException;
public class UnicodeUtil
{
/**
*
* @param gbString
* @return
*/
public static String encodeUnicode(final String data)
{
char[] utfBytes = data.toCharArray();
String unicodeBytes = "";
for (char utfByte : utfBytes)
{
String hexByte = Integer.toHexString(utfByte);
if (hexByte.length() <= 2)
{
hexByte = "00" + hexByte;
}
unicodeBytes += "//u" + hexByte;
}
return unicodeBytes;
}
public static String decodeUnicode(final String data)
{
int start = 0;
int end = 0;
final StringBuilder buffer = new StringBuilder();
while (start > -1)
{
end = data.indexOf("//u", start + 1);
String charStr = "";
if (end == -1)
{
charStr = data.substring(data.length() - 4, data.length());
}
else
{
charStr = data.substring(end - 4, end);
}
char letter = (char) Integer.parseInt(charStr, 16);
buffer.append(new Character(letter).toString());
start = end;
}
return buffer.toString();
}
public static void main(String[] args) throws UnsupportedEncodingException
{
String str = "非常抱歉,暂不支持该文件类型!";
String unicode = encodeUnicode(str);
System.out.println(unicode);
String decode = decodeUnicode(unicode);
System.out.println(decode);
}
}
import java.io.UnsupportedEncodingException;
public class UnicodeUtil
{
/**
*
* @param gbString
* @return
*/
public static String encodeUnicode(final String data)
{
char[] utfBytes = data.toCharArray();
String unicodeBytes = "";
for (char utfByte : utfBytes)
{
String hexByte = Integer.toHexString(utfByte);
if (hexByte.length() <= 2)
{
hexByte = "00" + hexByte;
}
unicodeBytes += "//u" + hexByte;
}
return unicodeBytes;
}
public static String decodeUnicode(final String data)
{
int start = 0;
int end = 0;
final StringBuilder buffer = new StringBuilder();
while (start > -1)
{
end = data.indexOf("//u", start + 1);
String charStr = "";
if (end == -1)
{
charStr = data.substring(data.length() - 4, data.length());
}
else
{
charStr = data.substring(end - 4, end);
}
char letter = (char) Integer.parseInt(charStr, 16);
buffer.append(new Character(letter).toString());
start = end;
}
return buffer.toString();
}
public static void main(String[] args) throws UnsupportedEncodingException
{
String str = "非常抱歉,暂不支持该文件类型!";
String unicode = encodeUnicode(str);
System.out.println(unicode);
String decode = decodeUnicode(unicode);
System.out.println(decode);
}
}
相关文章推荐
- C/C++ 各种字符编码字符串的转换 可用于XML文件的字符转化读写 (收集 GBK - UTF8 - Unicode - ANSI )
- python 字符串编码 str和unicode 区别以及相互转化 decode('utf-8') encode('utf-8')
- 字符串转化为unicode编码
- php度取excel内容 并对其unicode编码转化成gb2312
- C#2005中如何把unicode编码的数字转化为EBCDIC编码
- ANSI、UTF-8、UNICODE字符串相互转化
- 转化字符串的编码方式
- python 中 string 和 unicode 字符串编码问题
- 对字符串进行编码转化
- 怎样把字符串转化为 Utf8 编码,高手指点
- python编码处理:unicode字节串转成中文 各种字符串举例说明
- ASCII、Unicode、UTF-8 字符串和编码
- QT中Unicode编码与字符串之间的转换
- JavaScript中字符串与Unicode编码的互相转换
- Python中的字符串操作和编码Unicode详解
- json \u unicode字符串转化 c++
- Ansi、Unicode、UTF8等编码字符串之间的转换和写入文本文件(学习中……)
- python 2.x将raw_input()方式输入的字符串转换为unicode编码
- js数租,对象, 数组,字符串,函数,变量,int与String 型转化,String 转化为js 中内部代码,编码格式转化,<a>标签的死链接
- 使用python将用ASCII表示的16进制unicode编码的ASCII字符串转换为unicode字符串