为什么我的二进制文件代码不能正常工作?

时间:2014-08-24 00:14:47

标签: java binary converter

我正在尝试创建一个将英语转换为自定义二进制文件的翻译器,反之亦然。它将英语翻译成二进制,但不是二进制翻译成英语。请有人帮助我,告诉我我做错了什么?

我想输入类似“01000 00101 01100 01100 01111”的内容并获得“HELLO”作为输出。现在程序只允许我一次输入一个字符。例如,我输入“01000”,我得到了h。但是当我输入类似“01000 00101”的东西时,我得到同样的回报。我想知道如何解决这个问题。

以下是代码:

import java.util.Scanner;


public class Translator
{
    public static void main ( String [] args )

    {
        Scanner input = new Scanner( System.in );
        System.out.print( "Would you like to convert English to Binary (yes or no)? " );
        String answer = input.nextLine();
        if( answer.equals( "yes" ) )
        {
        System.out.println( "Enter English text here: " );
        String english = input.nextLine();
        System.out.println( stringToBinary( english ) );
        }
        if (answer.equalsIgnoreCase( "no" ) )
        {
            System.out.print( "Binary to English? " );
            String answer2 = input.nextLine();
            if (answer2.equalsIgnoreCase( "yes" ) )
            {
                System.out.println( "Enter Binary text here: " );
                // enter binary here
                String code = input.nextLine();
                System.out.println( stringToEnglish( code ) );
            }
        }
    }

    public static String encode (String toEncode)
    {
        String binary = toEncode;

        if (toEncode.equalsIgnoreCase("a"))
            binary = "00001";
        if (toEncode.equalsIgnoreCase("b"))
            binary = "00010";
        if (toEncode.equalsIgnoreCase("c"))
            binary = "00011";
        if (toEncode.equalsIgnoreCase("d"))
            binary = "00100";
        if (toEncode.equalsIgnoreCase("e"))
            binary = "00101";
        if (toEncode.equalsIgnoreCase("f"))
            binary = "00110";
        if (toEncode.equalsIgnoreCase("g"))
            binary = "00111";
        if (toEncode.equalsIgnoreCase("h"))
            binary = "01000";
        if (toEncode.equalsIgnoreCase("i"))
            binary = "01001";
        if (toEncode.equalsIgnoreCase("j"))
            binary = "01010";
        if (toEncode.equalsIgnoreCase("k"))
            binary = "01011";
        if (toEncode.equalsIgnoreCase("l"))
            binary = "01100";
        if (toEncode.equalsIgnoreCase("m"))
            binary = "01101";
        if (toEncode.equalsIgnoreCase("n"))
            binary = "01110";
        if (toEncode.equalsIgnoreCase("o"))
            binary = "01111";
        if (toEncode.equalsIgnoreCase("p"))
            binary = "10000";
        if (toEncode.equalsIgnoreCase("q"))
            binary = "10001";
        if (toEncode.equalsIgnoreCase("r"))
            binary = "10010";
        if (toEncode.equalsIgnoreCase("s"))
            binary = "10011";
        if (toEncode.equalsIgnoreCase("t"))
            binary = "10100";
        if (toEncode.equalsIgnoreCase("u"))
            binary = "10101";
        if (toEncode.equalsIgnoreCase("v"))
            binary = "10110";
        if (toEncode.equalsIgnoreCase("w"))
            binary = "10111";
        if (toEncode.equalsIgnoreCase("x"))
            binary = "11000";
        if (toEncode.equalsIgnoreCase("y"))
            binary = "11001";
        if (toEncode.equalsIgnoreCase("z"))
            binary = "11010";
        if (toEncode.equalsIgnoreCase(""))
            binary = " ";

        return binary;
    }


    public static String stringToBinary( String text )
    {

        String newText = "";
        String selectedChar;
        String convertedChar;
        for (int i = 0; i < text.length(); i++)
        {


            selectedChar = text.charAt(i) + "";


            convertedChar = encode(selectedChar);

            if (convertedChar.equals(" ")) 
            {
                newText = newText + " ";
            }

            else
            {
                newText = newText + convertedChar;
                if (!convertedChar.equals(" "))
                {
                    newText = newText + " ";
                }
            }
        }

        return newText;
    }

    public static String stringToEnglish( String text )
    {

        String english = text;

        if (text.equalsIgnoreCase("00001"))
            english = "a";
        if (text.equalsIgnoreCase("00010"))
            english = "b";
        if (text.equalsIgnoreCase("00011"))
            english = "c";
        if (text.equalsIgnoreCase("00100"))
            english = "d";
        if (text.equalsIgnoreCase("00101"))
            english = "e";
        if (text.equalsIgnoreCase("00110"))
            english = "f";
        if (text.equalsIgnoreCase("00111"))
            english = "g";
        if (text.equalsIgnoreCase("01000"))
            english = "h";
        if (text.equalsIgnoreCase("01001"))
            english = "i";
        if (text.equalsIgnoreCase("01010"))
            english = "j";
        if (text.equalsIgnoreCase("01011"))
            english = "k";
        if (text.equalsIgnoreCase("01100"))
            english = "l";
        if (text.equalsIgnoreCase("01101"))
            english = "m";
        if (text.equalsIgnoreCase("01110"))
            english = "n";
        if (text.equalsIgnoreCase("01111"))
            english = "o";
        if (text.equalsIgnoreCase("10000"))
            english = "p";
        if (text.equalsIgnoreCase("10001"))
            english = "q";
        if (text.equalsIgnoreCase("10010"))
            english = "r";
        if (text.equalsIgnoreCase("10011"))
            english = "s";
        if (text.equalsIgnoreCase("10100"))
            english = "t";
        if (text.equalsIgnoreCase("10101"))
            english = "u";
        if (text.equalsIgnoreCase("10110"))
            english = "v";
        if (text.equalsIgnoreCase("10111"))
            english = "w";
        if (text.equalsIgnoreCase("11000"))
            english = "x";
        if (text.equalsIgnoreCase("11001"))
            english = "y";
        if (text.equalsIgnoreCase("11010"))
            english = "z";
        if (text.equalsIgnoreCase(" "))
            english = " ";

        return english ;
    }

}

1 个答案:

答案 0 :(得分:3)

您的stringToEnglish方法只计算单个字符,因此它永远不会返回有效结果。你需要的是一个循环,StringBuilder以及比较中的一些变化。

首先,使用StringBuilder是构建字符串的最佳选择。字符串是不可变的,因此实际上每次添加一个时都会返回一个新实例。 StringBuilder是解决这个问题的方法。

public static String stringToEnglish( String text )
{
    StringBuilder builder = new StringBuilder();
    ...

我们还需要注意您有两个实例需要处理:

     
  1. 当你的字符串以&#34;开头时&#34;  
  2. 当你的字符串是二进制文件&#34; 00001&#34;

所以你需要处理多个字符的循环看起来像......

for (int i = 0; i < text.length() - 4; ++i) { 
...

优化是在它的二进制时跳转到字符串的末尾。 我们还需要考虑通过查看子字符串来迭代原始字符串......

if (text.charAt(i) == ' ') {
    continue;
} else if (text.substring(i, i + 5).equalsIgnoreCase("00001")){
    builder.append( "a" );   
}
...

首先,我们会看看它是否以" "开头,如果是,请继续。使用else if而不是ifs链来避免不必要的比较。如果没有,我们知道它是一个二进制表示,需要将其作为子字符串拉出来并根据你的表格进行测试&#34 ;;当你找到它时 - 追加它。

我们一直持续到我们能够走到尽头,然后我们将返回我们已建成的......

    return builder.toString() ;
}

您还可以switch使用break,或者更好地在评论中使用我的建议,而不是使用long if链。