两张照片,一道JAVA编程题,设计一个编程转换小工具,新手求大神

 我来答
中雇情轻会吃0Y
2015-06-10
知道答主
回答量:29
采纳率:0%
帮助的人:10.8万
展开全部
//汉字转区位码
public class Test {
public static String bytes2HexString(byte b) {
return bytes2HexString(new byte[] { b });
}

public static String bytes2HexString(byte[] b) {
String ret = "";
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase();
}
return ret;
}

public static void main(String[] args) throws UnsupportedEncodingException {
String str = "啊额";
byte[] bs = str.getBytes("GB2312");
String s = "";
System.out.println(bs.length);
for (int i = 0; i < bs.length; i++) {
int a = Integer.parseInt(bytes2HexString(bs[i]), 16);
s += (a - 0x80 - 0x20) + "";
}
System.out.println(s);
}
}

//字符ASCII码互转
public class Test2 {
public static void main(String[] args) throws Exception {
String str = "测";
System.out.println("字符转ASCII:"+showIntArray(string2ascii(str)," "));
System.out.println("ASCII转字符:"+ascii2String("27979"));
}

public static int[] string2ascii(String str) throws Exception {
if (str == null || "".equals(str)) {
return null;
}
char[] chars = str.toCharArray();
int[] asciiArray = new int[chars.length];
for (int i = 0; i < chars.length; i++) {
asciiArray[i] = (int)chars[i];
}
return asciiArray;
}

public static String ascii2String(String ASCIIs) {
String[] ASCIIss = ASCIIs.split(",");
StringBuffer sb = new StringBuffer();
for (int i = 0; i < ASCIIss.length; i++) {
sb.append((char)(Integer.parseInt(ASCIIss[i])));
}
return sb.toString();
}

public static String showIntArray(int[] intArray, String delimiter) {
StringBuffer sb=new StringBuffer();
for (int i = 0; i < intArray.length; i++) {
sb.append(intArray[i] + delimiter);
}
return sb.toString();
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式