简体   繁体   English

我希望能够根据ASCII十进制表将数字转换为文本

[英]I want to be able to convert numbers into text according to the ASCII decimal table

I am trying to make it so that I can take individual three-character substrings and convert them to integers under the conditions tht the length of the String is a multiple of three. 我试图做到这一点,以便我可以将单个的三个字符的子字符串转换为整数,条件是字符串的长度是三的倍数。 The integers into which the partioned substrings are converted are supposed to function as relative positions in an array that contains all the printing characters of the ASCII table. 应当将分割后的子字符串转换成的整数用作包含ASCII表的所有打印字符的数组中的相对位置。

 String IntMessage = result.toString();
   if 
   {
   (IntMessage.substring(0,1)=="1" && IntMessage.length()%3==0)
       for(j=0;j < IntMessage.length()-2;j += 3)
           n = Integer.parseInt(IntMessage.substring(j,j+3));
           mess += ASCII[n-32];
       return mess;

Under otherwise conditions, the method should take the first two characters of the String and initialize them to a variable i. 在其他情况下,该方法应采用String的前两个字符并将其初始化为变量i。 In this case, the variable mess is initialized to the character in the ASCII array with an index of i-32. 在这种情况下,变量mess被初始化为索引为i-32的ASCII数组中的字符。 Then there is a for loop that takes the remaining characters and partitions them into three-digit substrings and they are taken and changed into strings according to their corresponding positions in the ASCII array. 然后是一个for循环,它将剩余的字符分成三个数字的子字符串,然后根据它们在ASCII数组中的对应位置将其取为字符串。 The String variables in this array are continuously added on to the the variable mess in order to get the BigInteger to String conversion of the IntMessage String. 此数组中的String变量会连续添加到变量mess上,以便获得IntMessage字符串从BigInteger到String的转换。

   int i = Integer.parseInt(IntMessage.substring(0,2));

   mess=ASCII[i-32];
           for(l=2; l< IntMessage.length() - 2; l+=3)
               r = Integer.parseInt(IntMessage.substring(l,l+3));
               mess+=ASCII[r-32];
   return mess;

For some reason the method isn't working and I was wondering whether I was doing something wrong. 由于某种原因,该方法无法正常工作,我想知道自己是否做错了什么。 I know how to take an input String and convert it into a series of numbers but I want to do the opposite also. 我知道如何获取输入String并将其转换为一系列数字,但是我也想做相反的事情。 Is there anyway you could help? 无论如何,您可以提供帮助吗?

Based on your description you can use the following methods: 根据您的描述,您可以使用以下方法:

String fromIntMessage(String msg) {
    StringBuilder result = new StringBuilder();
    for (int x = (msg.length() % 3 - 3) % 3; x < msg.length(); x += 3) {
        int chr = Integer.parseInt(msg.substring(Math.max(x, 0), x + 3));
        result.append(Character.toString((char) (chr - 32)));
    }
    return result.toString();
}

String toIntMessage(String string) {
    StringBuilder result = new StringBuilder();
    for (char c : string.toCharArray()) {
        result.append(String.format("%03d", c + 32));
    }
    return result.charAt(0) == '0' ? result.substring(1) : result.toString();
}

which will give you 这会给你

toIntMessage("DAA")             // => "100097097"
fromIntMessage("100097097")     // => "DAA"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM