字符串转为unicode码的串和unicode码的串转为普通字符串(还原)
来源:互联网 发布:淘宝大数据分析报告 编辑:程序博客网 时间:2024/05/24 05:55
public class StrEncodeUtils {
/**
* 判断是否为中文字符
* @param c
* @return
*/
public static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
return false;
}
/**
* 将字符串转成unicode
* @param str 待转字符串
* @return unicode字符串
*/
public static String convert(String str){
str = (str == null ? "" : str);
String tmp;
StringBuffer sb = new StringBuffer(10000);
char c;
int i, j;
sb.setLength(0);
for (i = 0; i < str.length(); i++)
{
c = str.charAt(i);
sb.append("\\u");
j = (c >>>8); //取出高8位
tmp = Integer.toHexString(j);
if (tmp.length() == 1)
sb.append("0");
sb.append(tmp);
j = (c & 0xFF); //取出低8位
tmp = Integer.toHexString(j);
if (tmp.length() == 1)
sb.append("0");
sb.append(tmp);
}
return (new String(sb));
}
/**
* 将unicode 字符串
* @param str 待转字符串
* @return 普通字符串
*/
public static String revert(String unicodeStr){
unicodeStr = (unicodeStr == null ? "" : unicodeStr);
if (unicodeStr.indexOf("\\u") == -1)//如果不是unicode码则原样返回
return unicodeStr;
StringBuffer sb = new StringBuffer(1000);
for (int i = 0; i < unicodeStr.length() - 5;) //原来是6,总是少最后一个字符,现在改为5,好了
{
String strTemp = unicodeStr.substring(i, i + 6);
String value = strTemp.substring(2);
int c = 0;
for (int j = 0; j < value.length(); j++)
{
char tempChar = value.charAt(j);
int t = 0;
switch (tempChar)
{
case 'a':
t = 10;
break;
case 'b':
t = 11;
break;
case 'c':
t = 12;
break;
case 'd':
t = 13;
break;
case 'e':
t = 14;
break;
case 'f':
t = 15;
break;
default:
t = tempChar - 48;
break;
}
c += t * ((int) Math.pow(16, (value.length() - j - 1)));
}
sb.append((char) c);
i = i + 6;
}
return sb.toString();
}
}
/**
* 判断是否为中文字符
* @param c
* @return
*/
public static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
return false;
}
/**
* 将字符串转成unicode
* @param str 待转字符串
* @return unicode字符串
*/
public static String convert(String str){
str = (str == null ? "" : str);
String tmp;
StringBuffer sb = new StringBuffer(10000);
char c;
int i, j;
sb.setLength(0);
for (i = 0; i < str.length(); i++)
{
c = str.charAt(i);
sb.append("\\u");
j = (c >>>8); //取出高8位
tmp = Integer.toHexString(j);
if (tmp.length() == 1)
sb.append("0");
sb.append(tmp);
j = (c & 0xFF); //取出低8位
tmp = Integer.toHexString(j);
if (tmp.length() == 1)
sb.append("0");
sb.append(tmp);
}
return (new String(sb));
}
/**
* 将unicode 字符串
* @param str 待转字符串
* @return 普通字符串
*/
public static String revert(String unicodeStr){
unicodeStr = (unicodeStr == null ? "" : unicodeStr);
if (unicodeStr.indexOf("\\u") == -1)//如果不是unicode码则原样返回
return unicodeStr;
StringBuffer sb = new StringBuffer(1000);
for (int i = 0; i < unicodeStr.length() - 5;) //原来是6,总是少最后一个字符,现在改为5,好了
{
String strTemp = unicodeStr.substring(i, i + 6);
String value = strTemp.substring(2);
int c = 0;
for (int j = 0; j < value.length(); j++)
{
char tempChar = value.charAt(j);
int t = 0;
switch (tempChar)
{
case 'a':
t = 10;
break;
case 'b':
t = 11;
break;
case 'c':
t = 12;
break;
case 'd':
t = 13;
break;
case 'e':
t = 14;
break;
case 'f':
t = 15;
break;
default:
t = tempChar - 48;
break;
}
c += t * ((int) Math.pow(16, (value.length() - j - 1)));
}
sb.append((char) c);
i = i + 6;
}
return sb.toString();
}
}
0 0
- 字符串转为unicode码的串和unicode码的串转为普通字符串(还原)
- 中文转为unicode的字符串形式
- 字符串转为unicode
- Java程序将字符串转为unicode的方法
- Python将'\u'开头的字符串转为unicode编码
- 处理字符串 将字符串中的unicode字符转为汉字
- 将Unicode转为Ansi 字符的方法
- 字符转为Unicode编码的小方法
- Unicode编码的byte 数组转为String
- Java String字符串和Unicode字符相互转换代码(包括混有普通字符的Unicode)
- 将二进制转为字符串,再还原的方法
- Gb2312和Utf8 转为 Unicode
- python 将反斜杠u \u字符串转为unicode/utf8
- Java实现Unicode码转为中文
- Unicode和普通字符串之间转换
- Java中的中文字符串和Unicode码的相互转换
- Unicode 字符串的转换
- UniCode字符串的拷贝
- 编程小回忆五
- Android进程与内存及内存泄露
- 快速幂
- Mysql 免安装版配置
- oracle 常用sql
- 字符串转为unicode码的串和unicode码的串转为普通字符串(还原)
- 第七章 F# 库(二)
- Hadoop学习之路
- Java中的函数传递
- 设计模式——组合模式
- java 计算字符串中子串出现的次数
- usb协议总结
- AdTime:多屏时代下传统媒体的鼓起
- Tomcat自带log的配置