import java.io.*;public class StringCoder
{
public static final int BYTES_OF_CHAR = 4;
public static final int BYTES_OF_INTEGER = 4;
public static final int BYTES_OF_LONG = 8;
public static final int BYTES_OF_FLOAT = 4;
public static final int BYTES_OF_DOUBLE = 8; private static char [] hexCharArray = {'0','1','2','3',
'4','5','6','7',
'8','9','A','B',
'C','D','E','F'}; public static String decodeUTF8(String src)
{
String [] unitArray = src.split("%");
byte[] buffer = new byte[unitArray.length];
for (int i = 1; i < unitArray.length; i++)
{
buffer[i] = hexStringToByte(unitArray[i]);
}
return decodeUTF8(buffer,1,unitArray.length);
} public static String decodeUTF8(byte[] buffer,int begin,int end)
{
byte [] tmpBuf = new byte[5];
char [] chars = new char[end];
int leng = 0;
for (int i = begin; i < end;)
{
int data = 0;
tmpBuf[0] = buffer[i];
int byteNum = getLeftCountOf1InByte(tmpBuf[0]); for (int j = 1; j < byteNum; j++)
{
tmpBuf[j] = (byte)(buffer[i + j] & 0x3F);
}
tmpBuf[0] = maskFirstByte(tmpBuf[0]);
int byteCount = 0;
for (int j = byteNum - 1; j >= 0; j--)
{
data |= tmpBuf[j] << (6 * byteCount);
byteCount++;
}
chars[leng++] = (char)data;
i += byteNum;
}
return new String(chars,0,leng);
} public static byte hexStringToByte(String hexStr)
{
hexStr = hexStr.toUpperCase();
char high = hexStr.charAt(0);
char low = hexStr.charAt(1); int highVal = 0;
if (high >= '0' && high <= '9')
{
highVal = high - '0';
}
else
{
highVal = 10 + (high - 'A');
}
int lowVal = 0;
if (low >= '0' && low <= '9')
{
lowVal = low - '0';
}
else
{
lowVal = 10 + (low - 'A');
}
return (byte)((highVal << 4) | lowVal);
} public static int getLeftCountOf1InByte(byte b)
{
int count = 0;
int mask = 1 << 7;
for (int i = 0; i < 8; i++)
{
if ((b & mask) == 0)
{
break;
}
else
{
count++;
mask >>= 1;
}
}
return count;
} private static byte maskFirstByte(byte b)
{
int mask = 1 << 7;
for (int i = 0; i < 8; i++)
{
if ((b & mask) == 0)
{
break;
}
else
{
b &= ~mask;
mask >>= 1;
}
}
return (byte)b;
}
public static void main(String [] args) throws Exception
{
String src = "%e6%a1%8c%e9%9d%a2";
if (args.length > 0)
{
src = args[0];
}
String rs = new StringCoder().decodeUTF8(src);
System.out.println(rs);
}
}
解决方案 »
- 一道面试题,求更好的答案
- 菜鸟求解反射的好处
- class文件需要在新的机器上安装JRE后重新编译后才能运行吗?
- final用来修饰变量、方法、类分别代表什么含义?
- 关于向硬盘写入文件的!
- 一个问题,来者有分
- 请问有谁能帮小弟理解一下runtime properties内的含义
- 空指针异常问题
- 多线程下如何唤醒指定线程
- java中如何获取一个textfiled 文本框中的数字,并将之传入一个list中(动态 数组也可以)?
- 怎么读取文件对话框选择的txt文件 要一行一行的读取 并且添加到自己定义的GUI界面里面
- 编写一个应用程序,使用HashSet演示从ArrayList中删除重复的String。谢谢各位大侠!!我是初学者。
{
int count = 0;
int mask = 1 << 7;
for (int i = 0; i < 8; i++)
{
if ((b & mask) == 0)
{
break;
}
else
{
count++;
mask >>= 1;
}
}
return count == 0 ? 1 : count;
}
方法 getLeftCountOf1InByte 用来把首字节里的字节数信息读出来. 如E6 -> 3
方法 maskFirstByte 用来把首字节的提示信息部分去掉只留下数字部分 如E6 -> 00000110
有了这三个方法就大功告成啦
U+00000000 - U+0000007F: 0 xxxxxxx 0x - 7x
U+00000080 - U+000007FF: 110 xxxxx 10 xxxxxx Cx 8x - Dx Bx
U+00000800 - U+0000FFFF: 1110 xxxx 10 xxxxxx 10 xxxxxx Ex 8x 8x - Ex Bx Bx
U+00010000 - U+001FFFFF: 11110 xxx 10 xxxxxx 10 xxxxxx 10 xxxxxx F0 8x 8x 8x - F7 Bx Bx Bx
U+00200000 - U+03FFFFFF: 111110 xx 10 xxxxxx 10 xxxxxx 10 xxxxxx 10 xxxxxx F8 8x 8x 8x 8x - FB Bx Bx Bx Bx
U+04000000 - U+7FFFFFFF: 1111110 x 10 xxxxxx 10 xxxxxx 10 xxxxxx 10 xxxxxx 10 xxxxxx FC 8x 8x 8x 8x 8x - FD Bx Bx Bx Bx Bx
比如11100000
|
就指向这个位置