[英]java convert String of integers into possible combination of ASCII characters
Say that I have 说我有
String input = "Programming", result="\0";
int temp;
for (int i=0;i<input.length();++i) {
temp = input.charAt(i);
result += temp;
}
result
would be 8011411110311497109109105110103
. result
将是8011411110311497109109105110103
。 I know that 我知道
P = 80
r = 114
o = 111
g = 103
r = 114
a = 97
m = 109
m = 109
i = 105
n = 110
g = 103
Out of curiosity, is it possible, in easy way, to reverse the process? 出于好奇,是否有可能以简单的方式逆转这一过程?
what you would have to do is assume that Each number lies between either 'A' to 'Z', (65 to 90) or 'a' to 'z' (97 to 122) 您需要做的是假设每个数字都介于'A'至'Z'(65至90)或'a'至'z'(97至122)之间
substring()
substring()
获得前两位数字 it would be much simpler if you could have it be padded to three digits so you would know that every three digits formed an ASCII letter 如果您可以将其填充到三位数字,则将更加简单,因此您将知道每三位数字组成一个ASCII字母
code that works only if it is letters: 仅当是字母时才有效的代码:
public static void main(String[] args) {
String toConvert= "8011411110311497109109105110103";
String result="";
while(toConvert.length()>0){
String digits=toConvert.substring(0, 2);
int num=Integer.valueOf(digits);
if(('A'<=num&&num<='Z')||('a'<=num&&num<='z')){
toConvert=toConvert.substring(2);
}
else{
digits=toConvert.substring(0, 3);
num=Integer.valueOf(digits);
toConvert=toConvert.substring(3);
}
String letter = String.valueOf((char) num);
result+=letter;
}
System.out.println(result);
}
note if you change the test to num>25
, then it will correctly work for all ASCII values from 26 up: for values 26 to 99, it will interpret them as having 2 digits correctly, for values from 100 to 255 it will only look at the first 2 digits, so they will be seen as 10-25, and will be interpreted as 3 digits long 请注意,如果将测试更改为
num>25
,则它将对所有从26开始的ASCII值正确工作:对于26到99的值,它将正确地解释为具有2位数字,对于100到255的值将仅显示前两位数字,因此它们将被视为10-25,并将被解释为3位数字长
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.