展开全部
这题貌似无聊,不过细想也涉及很多JAVA的编程基础知识。
这位triompha的这段程序好像也没有完全按照提问者的要e68a843231313335323631343130323136353331333332613130求作答。
他这段程序的运行结果是:
所以我这里也写了一个,先看运行结果:
上代码:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Student{
public char[] name = new char[]{'无','聊','题'};
public int sid= 12123123;
public static void main(String[] args){
Student stu = new Student();
String strName = new String(stu.name);
System.out.println("1 name:" + strName + " sid:" + stu.sid);
System.out.println("2 Int2Hex:" + spaceStr(strName) + Integer.toHexString(stu.sid));
String uni = stringToUnicode(strName);
System.out.println("3 unicode:" + uni );
String cha = unicodeToString(uni);
System.out.println("4 change:" + cha + " unicode:" + stringToUnicode(cha) );
String sch = String.valueOf(stu.sid);
System.out.println("5 segment:" + cutSegment(cutSegment(sch,2,"-"),6,"-"));
}
//用于计算字符数并对其
public static String spaceStr(String s){
String str = "";
for (int i = 0; i
str += " ";
}
return str;
}
//转换Unicode
public static String stringToUnicode(String s) {
String str = "";
for (int i = 0; i
int ch = (int) s.charAt(i);
if (ch > 255)
str += "\\u" + Integer.toHexString(ch);
else
str += "\\" + Integer.toHexString(ch);
}
return str;
}
//转换字符,末位求反
public static String unicodeToString(String s) {
Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))"); //正则表达式
Matcher matcher = pattern.matcher(s);
char ch;
while (matcher.find()) {
ch = (char) (Integer.parseInt(matcher.group(2), 16) ^ 1);//这里做了最后一位求反的特殊处理^1
s = s.replace(matcher.group(1), ch + "");
}
return s;
}
//插入分隔符
public static String cutSegment(String s, int i, String strInsert){
StringBuffer buff = new StringBuffer(s);
buff.insert(i,strInsert);
return buff.toString();
}
}
代码差不多要60行,这还真不是一个简单的问题啊。