简体   繁体   English

java将整数字符串转换为ASCII字符的可能组合

[英]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)之间

  1. get the first 2 digits via a call to substring() 通过调用substring()获得前两位数字
  2. check if those lie within the above bounds by converting to a numeric type 通过转换为数值类型来检查它们是否在上述范围内
    • if they do then continue with the loop 如果他们这样做,则继续循环
  3. else get the third digit and check that 否则得到第三位数字并检查

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.

相关问题 Java:如何将ASCII字符串转换为字符字符串? - Java: How to convert String of ASCII to String of characters? 有没有将字符串中的整数转换为 JAVA 中的 ASCII 字符的解决方案? - Is there any Solution for Converting integers in string to its ASCII characters in JAVA? 在Java中将包含字符的字符串转换为整数 - convert string that contains characters into integers in java 获取包含Java中重复字符的字符串或组合的所有可能排列 - Getting every possible permutation of a string or combination including repeated characters in Java 输入为字符串的Java ASCII字符 - Java ASCII Characters Inputting As A String 在Java中将十六进制字符串转换为ASCII - Convert a String of Hex into ASCII in Java 如何在Android中将ASCII字符转换为字符串? - How to convert ASCII characters to string in Android? 将ascii字符集转换回字符串 - Convert set of ascii characters back to string 将十六进制字节数组的字符串表示形式转换为 Java 中的非 ascii 字符的字符串 - Convert string representation of a hexadecimal byte array to a string with non ascii characters in Java 如何在Java中从具有ASCII字符串组合的字符串中删除反斜杠(“ \\”) - How to remove backslash(“\”) from string having ASCII string combination in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM