从ASCII解码Java

时间:2015-11-30 18:39:35

标签: java ascii decoding

我已经设置了一个与ASCII有关的自定义编码。它如下:

  1. 所有小写字母都转换为大写字母。
  2. 编码消息中的第一个字母存储为其ASCII代码值。
  3. 所有后续字母均表示为当前字母与上一个字母之间的偏移量。
  4. 修改 例如:“cat”= 67 -2 19,因为“C”= 67,“A”是距离“C”2个字母,“T”是距离“A”19个字母。我的目标是从用户获取String输入,将其转换为数字数组,然后让这些数字在解码过程中运行并打印出用户输入的密码。以下是我目前的工作:

    import java.util.Scanner;
    public class DecodeFromAscii {
        public static void main (String args[]) {
            Scanner input = new Scanner(System.in);
            System.out.println("How many characters long is your encoded message?:");
            int characters = input.nextInt();
            System.out.println("Enter your encoded message on a single line:");
            String encodedMessage = input.next();
            String[] items = encodedMessage.split(",");
            int[] numbers = new int[items.length];
            int i = 0; 
            int lastNumber = 0;
            for (int n : numbers) {
                if (i == 0) {
                    System.out.print((char)n);
                    lastNumber = n;
                } else {
                    lastNumber = n + lastNumber;
                    System.out.print((char)(lastNumber));
                }
                i++;
            }
        }
    }
    

2 个答案:

答案 0 :(得分:0)

所以你有一个想要解码的整数数组..

int previous = -1;
for(num in array){
  if(previous == -1) print(decode(num));  // first char ascii
  else{
    print(decode(previous+num));  
  }
  previous=num;
}

我认为这样的事情应该有效

在你的例子中,给出一个数组67 -2 19

loop1: previous is -1 so it decodes 67 giving "C", previous becomes 67
loop2: it decodes previous-num (67-2=65) giving "A", previous becomes
loop3: it decodes previous-num (65+19=84) giving "T"

答案 1 :(得分:0)

你做得太复杂了,只需将前一个字符减去解码或投射到char进行编码......

public class DecodeFromAscii {
    public static void main (String args[]) {
        int number = 65; //I'm too lazy to read from user input... so here's A ASCII value (65)
        char numberAsChar = (char) number; //Convert 65 to it's ASCII value (A)
        String word = "cat"; //The word to decode (read it from user input, I'm lazy)
        int i = 0; //A counter
        char previousChar = 'a'; //Just needed to initialize to something
        word = word.toUpperCase(); //Convert word to upper case to get the expected results as C=67
        System.out.println(numberAsChar); //Prints 'A'
        for (char c : word.toCharArray()) {
            if (i == 0) {
                System.out.println((int)c); //To get the first char's ASCII code
                previousChar = c; //So we can do the operations to get -2 and 19
            } else {
                System.out.println(c - previousChar); //It does the operation to get -2 and 19
                previousChar = c; //To get 19 
            }
            i++;
        }
    }
}

输出:

A
67
-2
19

修改

截至您的comment

  

很抱歉这个混乱。我正在寻找用户在一行输入数字(例如:67 -2 19),然后系统会认为62 = C,C中的-2个字母是A,A中的19个字母是T.然后它会打印出来" Cat"。

遵循相同的逻辑:

public class DecodeFromAscii {
    public static void main (String args[]) {
        int numbers [] = {67, -2, 19}; //The user input
        int i = 0; //Counter
        int lastNumber = 0; //Our last number
        for (int n : numbers) {
            if (i == 0) {
                System.out.print((char)n); //Print the 1st letter
                lastNumber = n;
            } else {
                lastNumber = n + lastNumber; //Do operations (if negative, then it will substract, if possitive, it will sum).
                System.out.print((char)(lastNumber)); //Print the result
            }
            i++; //Increase counter
        }
        System.out.println(""); //Just a line break
    }
}

这种情况下的输出将是:

CAT

EDIT2

在您更新了代码之后,由于您从未在数组numbers中设置任何数字,因此无法获得输出,为此,您需要使用{{1}调用:

try-catch

所以,你的最终代码可能就是这个:

try {
    for (String s : items) {
        numbers[i] = Integer.parseInt(s); //Convert String to int and fill array.
        i++;
    }
} catch (NumberFormatException nfe) {
    nfe.printStackTrace();
}

输出到:

import java.util.Scanner;
public class DecodeFromAscii {
    public static void main (String args[]) {
        Scanner input = new Scanner(System.in);
        System.out.println("How many characters long is your encoded message?:");
        int characters = input.nextInt();
        System.out.println("Enter your encoded message on a single line:");
        String decodeMessage = input.next();
        String[] items = decodeMessage.split(",");
        int i = 0; 
        int numbers[] = new int [items.length];
        try {
            for (String s : items) {
                numbers[i] = Integer.parseInt(s);
                i++;
            }
            int lastNumber = 0;
            for (int n : numbers) {
                if (i == 0) {
                    System.out.print((char)n);
                    lastNumber = n;
                } else {
                    lastNumber = n + lastNumber;
                    System.out.print((char)(lastNumber));
                }
                i++;
            }
            System.out.println("");
        } catch (NumberFormatException nfe) {
            nfe.printStackTrace();
        }
    }
}

但请记住,使用此方法时,可能会出现一些不可打印的ASCII符号......