public class Unicode2GB
{
public Unicode2GB()
{
}
public static String decode(String s)
{
StringBuffer sb = new StringBuffer();
boolean escape = false;
for(int i=0; i<s.length(); i++)
{
char c = s.charAt(i);
switch (c)
{
case '\\':
escape = true;
break;
case 'u':
case 'U':
if (escape)
{
try
{
sb.append((char)Integer.parseInt(
s.substring(i+1,i+5),16));
escape = false;
}
catch (NumberFormatException e) {
throw new IllegalArgumentException();
}
i += 4;
}
else
{
sb.append(c);
}
break;
default:
sb.append(c);
break;
}
} return sb.toString();
} public static void main(String[] args) throws IOException
{
System.out.println("Please input string to be translated");
String oldStr = null;
BufferedReader in = new BufferedReader(new InputStreamReader
(System.in));
while(true)
{
oldStr = in.readLine();
System.out.println(Unicode2GB.decode(oldStr));
}
}
}
{
public Unicode2GB()
{
}
public static String decode(String s)
{
StringBuffer sb = new StringBuffer();
boolean escape = false;
for(int i=0; i<s.length(); i++)
{
char c = s.charAt(i);
switch (c)
{
case '\\':
escape = true;
break;
case 'u':
case 'U':
if (escape)
{
try
{
sb.append((char)Integer.parseInt(
s.substring(i+1,i+5),16));
escape = false;
}
catch (NumberFormatException e) {
throw new IllegalArgumentException();
}
i += 4;
}
else
{
sb.append(c);
}
break;
default:
sb.append(c);
break;
}
} return sb.toString();
} public static void main(String[] args) throws IOException
{
System.out.println("Please input string to be translated");
String oldStr = null;
BufferedReader in = new BufferedReader(new InputStreamReader
(System.in));
while(true)
{
oldStr = in.readLine();
System.out.println(Unicode2GB.decode(oldStr));
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货