所以我的代码只适用于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;
}
我需要修什么?我试过很多东西,但都没用,我真的需要一些帮助(注意:我有时会打印一些东西来帮助我找出哪里出了问题,但在这里我不知道哪里出了问题
3条答案
按热度按时间jjhzyzn01#
看起来reto的答案将大大简化您的代码,但仍然一次只解析一个字节。所以,我将把它封装在一个**parsebyte(stringbytestr)**方法中。然后,使用一个简单的for循环,以8为间隔遍历整个二进制字符串。最后的代码如下所示。
它将输出
qeeaahzv2#
输出:
efzxgjgh3#
如果要将一串位转换为其ascii等效值,可以执行以下操作。
获取字节,一次8位。
转换为
int
然后把它扔给char
然后打印出来将字符串的位减少8,然后重复,直到字符串为空。
退货