如何制作二进制翻译程序?

4urapxun  于 2021-06-26  发布在  Java
关注(0)|答案(3)|浏览(276)

所以我的代码只适用于16个数字长的二进制代码,但是如何使它适用于可能更长的二进制代码呢?
以下是我的方法:

public String binaryToText(String binary)
{
    int value = 1;
    String letter = "";
    String eightBitCode = "";
    int numberValue = 0;
    int i = 0;
    for (i = 7; i > 0; i--)
    {
        eightBitCode = eightBitCode + binary.charAt(i);

        if (binary.charAt(i) == '1')
        {
            numberValue = numberValue + value;

        }
        value = value * 2;
    }
    if (numberValue == 65)
    {
        letter = letter + "A";
    }
    if (numberValue == 66)
    {
        letter = letter + "B";
    }
    if (numberValue == 67)
    {
        letter = letter + "C";
    }
    if (numberValue == 68)
    {
        letter = letter + "D";
    }
    if (numberValue == 69)
    {
        letter = letter + "E";
    }
    if (numberValue == 70)
    {
        letter = letter + "F";
    }
    if (numberValue == 71)
    {
        letter = letter + "G";
    }
    if (numberValue == 72)
    {
        letter = letter + "H";
    }
    if (numberValue == 73)
    {
        letter = letter + "I";
    }
    if (numberValue == 74)
    {
        letter = letter + "J";
    }
    if (numberValue == 75)
    {
        letter = letter + "K";
    }
    if (numberValue == 76)
    {
        letter = letter + "L";
    }
    if (numberValue == 77)
    {
        letter = letter + "M";
    }
    if (numberValue == 78)
    {
        letter = letter + "N";
    }
    if (numberValue == 79)
    {
        letter = letter + "O";
    }
    if (numberValue == 80)
    {
        letter = letter + "P";
    }
    if (numberValue == 81)
    {
        letter = letter + "Q";
    }
    if (numberValue == 82)
    {
        letter = letter + "R";
    }
    if (numberValue == 83)
    {
        letter = letter + "S";
    }
    if (numberValue == 84)
    {
        letter = letter + "T";
    }
    if (numberValue == 85)
    {
        letter = letter + "U";
    }
    if (numberValue == 86)
    {
        letter = letter + "V";
    }
    if (numberValue == 87)
    {
        letter = letter + "W";
    }
    if (numberValue == 88)
    {
        letter = letter + "X";
    }
    if (numberValue == 89)
    {
        letter = letter + "Y";
    }
    if (numberValue == 90)
    {
        letter = letter + "Z";
    }
    //0        1      0      0     1     0     0    0
    //128     64     32     16     8     4     2    1
    i = 8;
    int h = 0;
    numberValue = 0;
    for (int j = 8; j < binary.length(); j++)
    {

        numberValue = 0;
        value = 1;
        j = j + 7;
        eightBitCode = "";
        h = binary.length() - 1;
        for (i = i; i < binary.length(); i++)
        {
            if (h == 7)
            {
                break;
            }
            eightBitCode = eightBitCode + binary.charAt(h);
            if (binary.charAt(h) == '1')
            {
                numberValue = numberValue + value;
            }
            value = value * 2;
            h = h - 1;
        }
    }
    if (numberValue == 65)
    {
        letter = letter + "A";
    }
    if (numberValue == 66)
    {
        letter = letter + "B";
    }
    if (numberValue == 67)
    {
        letter = letter + "C";
    }
    if (numberValue == 68)
    {
        letter = letter + "D";
    }
    if (numberValue == 69)
    {
        letter = letter + "E";
    }
    if (numberValue == 70)
    {
        letter = letter + "F";
    }
    if (numberValue == 71)
    {
        letter = letter + "G";
    }
    if (numberValue == 72)
    {
        letter = letter + "H";
    }
    if (numberValue == 73)
    {
        letter = letter + "I";
    }
    if (numberValue == 74)
    {
        letter = letter + "J";
    }
    if (numberValue == 75)
    {
        letter = letter + "K";
    }
    if (numberValue == 76)
    {
        letter = letter + "L";
    }
    if (numberValue == 77)
    {
        letter = letter + "M";
    }
    if (numberValue == 78)
    {
        letter = letter + "N";
    }
    if (numberValue == 79)
    {
        letter = letter + "O";
    }
    if (numberValue == 80)
    {
        letter = letter + "P";
    }
    if (numberValue == 81)
    {
        letter = letter + "Q";
    }
    if (numberValue == 82)
    {
        letter = letter + "R";
    }
    if (numberValue == 83)
    {
        letter = letter + "S";
    }
    if (numberValue == 84)
    {
        letter = letter + "T";
    }
    if (numberValue == 85)
    {
        letter = letter + "U";
    }
    if (numberValue == 86)
    {
        letter = letter + "V";
    }
    if (numberValue == 87)
    {
        letter = letter + "W";
    }
    if (numberValue == 88)
    {
        letter = letter + "X";
    }
    if (numberValue == 89)
    {
        letter = letter + "Y";
    }
    if (numberValue == 90)
    {
        letter = letter + "Z";
    }
    return letter;
}

我需要修什么?我试过很多东西,但都没用,我真的需要一些帮助(注意:我有时会打印一些东西来帮助我找出哪里出了问题,但在这里我不知道哪里出了问题

jjhzyzn0

jjhzyzn01#

看起来reto的答案将大大简化您的代码,但仍然一次只解析一个字节。所以,我将把它封装在一个**parsebyte(stringbytestr)**方法中。然后,使用一个简单的for循环,以8为间隔遍历整个二进制字符串。最后的代码如下所示。

public class BinaryParser {

        public static void main(String[] args) {
            BinaryParser parser = new BinaryParser();
            System.out.println(parser.parseString("010010000110010101101100011011000110111100100000010101110110111101110010011011000110010000100001"));
        }

        public String parseString(String binaryStr) {
                String result = "";
                // note the i += 8, which will add 8 to i each iteration.
                for (int i = 0; i < binaryStr.length(); i += 8) {
                        String byteStr = binaryStr.substring(i, i+8);
                        result += parseByte(byteStr);
                }
                return result;
        }

        // Courtesy of Reto. See link above.
        public char parseByte(String byteStr) {
                int decimal = Integer.parseInt(byteStr, 2);
                char letter = (char) decimal;
                return letter;
        }

    }

它将输出

Hello World!
qeeaahzv

qeeaahzv2#

String byteString = "01000001";
int decimal = Integer.parseInt(byteString, 2);
System.out.println("decimal: " + decimal);
char letter = (char) decimal;
System.out.println("letter: " + letter);

输出:

decimal: 65
letter: A
efzxgjgh

efzxgjgh3#

如果要将一串位转换为其ascii等效值,可以执行以下操作。

String bits = "0101010001101000011010010111001100"+
        "1000000111011101100001011100110010000001100010"+
        "01101001011011100110000101110010011110010010111000001010";

获取字节,一次8位。
转换为 int 然后把它扔给 char 然后打印出来
将字符串的位减少8,然后重复,直到字符串为空。

public static String convert(String bits) {
    if (bits.length() % 8 != 0) {
        throw new IllegalArgumentException(
                "String length must be divisible by 8");
    }
    StringBuilder sb = new StringBuilder();
    while (!bits.isEmpty()) {
        String byt = bits.substring(0, 8);
        sb.append((char) (Integer.valueOf(byt, 2).intValue()));
        bits = bits.substring(8);
    }
    return sb.toString();
}

退货

This was binary.

相关问题