将ascii值转换回字符

时间:2012-07-03 15:29:09

标签: java char int ascii

确定 我有一个字符串,其中包含从0到9的数字的ascii值 我希望把这个字符串并将其分解为原始值

String应该看起来像这样:

52 52 54 49 - 空格也是字符串中的字符

我想取数字45,现在由2个字符4和5构建 我想把它们恢复到原来的值4,但我无法弄清楚如何

这是我尝试这样做的示例代码 -

String st = "43 54 21";
int first=st.charAt(0);
int second=st.charAt(1);
int total=first*10+second;

System.out.println((char)total);

2 个答案:

答案 0 :(得分:0)

void convertToString()
{
    char redo;
    int letter;
    int length;
    do{
    cout<< "How long is your word \n";
    cin >> length;

    cout << "Type in the letter values \n";

        for (int x = 0; x < length; x++)
    {
        cin >> letter;
        cout << char (letter);
    }
        cout << "\n To enter another word hit R" << endl;
        cin >> redo;
    }while (redo == 'R');

    }

答案 1 :(得分:-1)

您可以使用扫描仪实现此目的:

public String parseBytes(String s){
 StringBuilder sb = new StringBuilder(1000);
 Scanner scanner = new Scanner(s);
 while(scanner.hasNext()){
  char ch = (char)scanner.nextInt();
  sb.append(ch);
 }
return sb.toString();
}