两张照片,一道JAVA编程题,设计一个编程转换小工具,新手求大神
展开全部
//汉字转区位码
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();
}
}
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();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询