在Java中,如何将ASCII码转换为整数?

在Java中,如何将ASCII码转换为整数?,java,encoding,Java,Encoding,假设我有一个字节整数数组。如何从这些ASCII码返回到真实世界的整数 例如,如果我们读取一个简单的整数文本文件,如下所示: 1 2 3 4 5 6 7 8 9 10 …放入一个字节数组,如下所示: boolean empty = true; while (( readChars = is.read(c)) != -1) { for (int i = 0; i < readChars; ++1) {

假设我有一个
字节
整数数组。如何从这些ASCII码返回到真实世界的整数

例如,如果我们读取一个简单的整数文本文件,如下所示:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
…放入一个字节数组,如下所示:

boolean empty = true;
while (( readChars = is.read(c)) != -1) {
  for (int i = 0; i < readChars; ++1) {
    Byte b = c[i];
    int xx = b.intValue();
    lastLine = xx;

    if (c[i] == '\n'){
      ++count;
      empty = true;
    } else {
        empty = false;
    }

  }
  }
  if (!empty) {
    count++;  
  }
布尔空=真;
而((readChars=is.read(c))!=-1){
对于(int i=0;i
然后,一旦该文件(只是普通整数)被放入字节数组中。。如果我们再次尝试将其打印回屏幕,它将不会以数字5的形式打印,而是以ASCII码的形式打印,即
53

我只是想了解一下这个编码主题,有什么建议吗?谢谢


谢谢

您可以将
char
转换为
int
。大概

char[] chars = "12345".toCharArray();
for (char ch : chars) {
    System.out.printf("%c = %d%n", ch, (int) ch);
}
输出为

1 = 49
2 = 50
3 = 51
4 = 52
5 = 53

您可以从
char
转换为
int
。大概

char[] chars = "12345".toCharArray();
for (char ch : chars) {
    System.out.printf("%c = %d%n", ch, (int) ch);
}
输出为

1 = 49
2 = 50
3 = 51
4 = 52
5 = 53
试试这个:

int asciiValue = 53;
int numericValue = Character.getNumericValue(asciiValue);

System.out.println(numericValue);
试试这个:

int asciiValue = 53;
int numericValue = Character.getNumericValue(asciiValue);

System.out.println(numericValue);

为什么要将其读入
字节[]
?读取文本文件的
String
char
有什么问题?为什么要将其读入
字节[]
?读取文本文件的
String
char
有什么不对?该方法的优点在于,它不仅适用于ASCII数字,还适用于花哨的Unicode内容以及字母(“a”=10,“B”=11,等等)。如果这不是您想要的,则需要注意。@dev,它可以工作,但如果最后一个数字是2位数,则只能得到1位数!!该方法的优点在于,它不仅适用于ASCII数字,还适用于花哨的Unicode内容以及字母(“A”=10,“B”=11,等等)。如果这不是您想要的,则需要注意。@dev,它可以工作,但如果最后一个数字是2位数,则只能得到1位数!!嗨,艾略特,我更新了我的问题。我不确定我的新代码是否能帮助你了解我在做什么,thnakshi Elliott,我更新了我的问题。我不确定我的新代码是否能帮助你了解我在做什么,thnaks