在这篇文章做一些 java中String类型与Ascii相互转化的知识点
一般转换的话是字符串中的一个一个字符转化
int i=Integer.valueOf(c);//c是字符串中的一个字符
char c=(char)x;//x是ASCII码
字符串转Ascii
public static voidConvertToASCII()
{ String string = "193521.641";
StringBuilder sb = new StringBuilder();
char[] ch = string.toCharArray();
for (int i = 0; i < ch.length; i++) {
sb.append(Integer.valueOf(ch[i]).intValue()).append(" ");// 加空格
// sb.append(Integer.valueOf(ch[i]).intValue());// 不加空格
System.out.println(sb.toString());}
}
Ascill转字符串
public static void ASCIIToConvert(){
String value = "49 57 51 53 50 49 46 54 52 49 ";
StringBuffer sbu = new StringBuffer();
String[] chars = value.split(" ");
for (int i = 0; i < chars.length; i++) {
sbu.append((char) Integer.parseInt(chars[i]));
}
System.out.println(sbu.toString());
}
附上洛谷一道题
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = "";
int[] arr = new int[100];
int max = 0;
for (int i = 0; i < 4; i++) {
str = str + sc.nextLine();
}
for (int i = 0; i < str.length(); i++) {
int temp = Integer.valueOf(str.charAt(i));
if (temp >= 65 && temp <= 90) {
arr[temp]++;
}
if (arr[temp] > max) {
max = arr[temp];
}
}
for(int i=max;i>0;i--) {
for(int x=65;x<=90;x++) {
if(arr[x]==i) {
System.out.print("* ");
arr[x]--;
}else {
System.out.print(" ");
}
}System.out.println();
}
System.out.println("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
}
}