简体   繁体   English

如何创建编码器? 解码器?

[英]How to create an encoder? Decoder?

I am fairly new to programming in general and I was wondering how I could encode/decode text that is inputted.一般来说,我对编程还很陌生,我想知道如何编码/解码输入的文本。

All letters must be brought down 3 letters for ex A -> DB -> E and so forth所有字母都必须减少 3 个字母,例如 A -> DB -> E 等等

Ill put in some pseudocode for an example:举一些伪代码为例:

INPUT MESSAGE: "LORYHBRX"输入消息:“LORYHBRX”

Encoded Message:LORYHBRX编码信息:LORYHBRX

Decoded Message:ILOVEYOU解码信息:ILOVEYOU

OUTPUT MESSAGE: "ILOVEYOU"输出信息:“我爱你”

Please help.请帮忙。

So far I have到目前为止我有

import java.util.*;

public class Encoder {

public static void main(String[] args) 
{   

    String a = "d";
    String b = "e";
    String c = "f";
    String d = "g";
    String e = "h";
    String f = "i";
    String g = "j";
    String h = "k";
    String i = "l";
    String j = "m";
    String k = "n";
    String l = "o";
    String m = "p";
    String n = "q";
    String o = "r";
    String p = "s";
    String q = "t";
    String r = "u";
    String s = "v";
    String t = "w";
    String u = "x";
    String v = "y";
    String w = "z";
    String x = "a";
    String y = "b";
    String z = "c";

    Scanner in = new Scanner(System.in);
    System.out.println("Please enter the text you wish to encode.");
    String place = in.nextLine();
    System.out.println(place);

}
}

I'm trying to convert what is inputted into the variables above.我正在尝试将输入的内容转换为上述变量。

I think I am stating all those strings as variable but I do not know how to make them changeable by the input.我想我将所有这些字符串声明为变量,但我不知道如何通过输入使它们可变。

I will give you some hints how to do decoding and encoding will be just the reverse process.我会给你一些提示如何进行解码和编码将只是相反的过程。

First, you should know computer can only understand numbers which called Ascii code .首先,您应该知道计算机只能理解称为Ascii code数字。

Ascii code is numerical representation of character like b , & , A , and so on, as result, capital and small letters have Ascii code which is int type. Ascii 代码是字符的数字表示,如b&A等,因此大写和小写字母都具有 int 类型的Ascii code

Read and See Ascci table here在此处阅读并查看 Ascci 表

Another subject you should know is Casting你应该知道的另一个主题是Casting

Casting is converting a type to another type like converting int to char or vice versa, but you should know casting some type cannot to convert to another type like type boolean to int which is impossible .铸造是一种类型转换为另一种类型的转换一样INT到char或反之亦然,但你应该知道一些铸造类型不能转换到为INT另一种类型的样型布尔这是不可能的。

Read about casting in Java阅读有关在 Java 中进行强制转换的信息

Let talk about Encoding and leave decoding to you to figure it out because it is reverse of encoding.让我们谈谈编码并将解码留给您自己解决,因为它与编码相反。

Hints about how encode关于如何编码的提示

  1. There is no need to define String like String a = "a" ;不需要像String a = "a"那样定义 String ;
  2. Since you learn about Ascci codes which are numbers, you can for loop through 97 to 122 with casting to get small chars.由于您了解了作为数字的 Ascci 代码,您可以通过强制转换来循环 97 到 122 以获得小字符。

    Example:例子:

     System.out.println("Asci code of small a is " + (int)'a' + "\\nsmall a is " + (char)97);

    output:输出:

     Asci code of small a is 97 small a is a

Note: you cast char to int and cast int to char.注意:您将 char 转换为 int 并将 int 转换为 char。

  1. Since you are dealing with numbers, so you can do addition.由于您正在处理数字,因此您可以进行加法。

    Example:例子:

     System.out.println("Three char after a is " + (char)(97+3));

    Ouput:输出:

     there char after a is d

for char x,y,and z,you can subtract 23 from their asci code , for example 120-23 is gonna give asci code for a.对于字符 x、y 和 z,您可以从它们的 asci 代码中减去 23,例如120-23将给出 a 的 asci 代码。

The Whole Code of Encoding and Decoding编码和解码的完整代码

import java.util.Scanner;

public class coder {

    public static void main(String[] args) {
        String[] qe = {"1 ","2 ","3 ","4 ","5 ","6 ","7 ","8 ","9 ","10 ","11 ","12 ","13 ",
            "14 ","15 ","16 ","17 ","18 ","19 ","20 ","21 ","22 ","23 ","24 ","25 ","26 "};
        String [] eq = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o",
                "p","q","r","s","t","u","v","w","x","y","z"};
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter the text you wish to encode.");
        String place = in.nextLine();
        place = place.toLowerCase();
        place = place.replace("a", qe[0]);
        place = place.replace("b", qe[1]);
        place = place.replace("c", qe[2]);
        place = place.replace("d", qe[3]);
        place = place.replace("e", qe[4]);
        place = place.replace("f", qe[5]);
        place = place.replace("g", qe[6]);
        place = place.replace("h", qe[7]);
        place = place.replace("i", qe[8]);
        place = place.replace("j", qe[9]);
        place = place.replace("k", qe[10]);
        place = place.replace("l", qe[11]);
        place = place.replace("m", qe[12]);
        place = place.replace("n", qe[13]);
        place = place.replace("o", qe[14]);
        place = place.replace("p", qe[15]);
        place = place.replace("q", qe[16]);
        place = place.replace("r", qe[17]);
        place = place.replace("s", qe[18]);
        place = place.replace("t", qe[19]);
        place = place.replace("u", qe[20]);
        place = place.replace("v", qe[21]);
        place = place.replace("w", qe[22]);
        place = place.replace("x", qe[23]);
        place = place.replace("y", qe[24]);
        place = place.replace("z", qe[25]);
        System.out.println(place);

        place = place.replace("26 ", eq[25]);
        place = place.replace("25 ", eq[24]);
        place = place.replace("24 ", eq[23]);
        place = place.replace("23 ", eq[22]);
        place = place.replace("22 ", eq[21]);
        place = place.replace("21 ", eq[20]);
        place = place.replace("20 ", eq[19]);
        place = place.replace("19 ", eq[18]);
        place = place.replace("18 ", eq[17]);
        place = place.replace("17 ", eq[16]);
        place = place.replace("16 ", eq[15]);
        place = place.replace("15 ", eq[14]);
        place = place.replace("14 ", eq[13]);
        place = place.replace("13 ", eq[12]);
        place = place.replace("12 ", eq[11]);
        place = place.replace("11 ", eq[10]);
        place = place.replace("10 ", eq[9]);
        place = place.replace("9 ", eq[8]);
        place = place.replace("8 ", eq[7]);
        place = place.replace("7 ", eq[6]);
        place = place.replace("6 ", eq[5]);
        place = place.replace("5 ", eq[4]);
        place = place.replace("4 ", eq[3]);
        place = place.replace("3 ", eq[2]);
        place = place.replace("2 ", eq[1]);
        place = place.replace("1 ", eq[0]);
        System.out.println(place);
    }

}

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

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