0

这是我无法绕开的家庭作业。我必须手动完成,所以不能使用“getBytes()”。另外,我必须先转换为十进制格式,然后再将十进制转换为 ASCII(例如 {0,1,1,0,0,0,1,0} = 98 为十进制格式,即 'b') . 我已将二进制代码排列成一个数组,并希望使用 for 循环逐个位置遍历数组。但是,我不确定我是否为 for 循环使用了正确的参数,也不确定如何将代码分成“8”位。另一件事,如何将十进制值转换为 ASCII?我是否应该列出我知道我会得到的所有可能的字母,然后使用 if-else 循环引用它们?或者我可以将小数转换为字符吗?到目前为止,这是我的代码......(有点乱,抱歉)

class Decoder
{
    public void findCode(Picture stegoObj)
    {
    Pixel pixTar = new Pixel(stegoObj,0,0);
    Pixel [] pixelArray = stegoObj.getPixels();
    int blueVal = 0;

    for(int length = 0; length < pixelArray.length; length++)
    {
        blueVal = pixTar.getBlue();                
    }
    System.out.println(blueVal);
    stegoObj.explore();
    }

    public void decode(int [] binary)
    {
        int binaryLen = binary.length;
        int totVal = 0;
        int newVal = 0;
        int bitVal = 0;

        for(int x = binaryLen - 1; x >= 0; x--)
        {
            bitVal = binary[x];
            int exp = x - (binaryLen - 1);
            totVal += (pow(bitVal, exp));
        }

        System.out.println(totVal);
     }
}
public class DecodeImage
{
    public static void main(String[] args)
    {
        Picture stegoObj = new Picture("SecretMessage.bmp");
        Decoder deco = new Decoder();
        int[] binArray =     {0,1,0,1,0,1,0,0,0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,1,1,0,1,1,1,0,1,1,0,1,1,1,1,0,1,1,1,0,0,1,0,0,1,1,0,1,1,0,0,0,1,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,1,0,1,1,1,0,0,1,1,0,0,1,0,0,0,0,0,0,1,1,0,1,1,0,1,0,1,1,0,1,0,0,1,0,1,1,0,1,1,1,0,0,1,1,0,0,1,0,1,0,0,0,0,1,1,0,1,0,0,0,0,1,0,1,0};
        //int[] binArray = {0,1,1,0,0,0,1,0};
        //deco.findCode(stegoObj);
        deco.decode(binArray);        
    }
}

编辑:

好的,所以我想出了这么多,在解码器类下,在解码块中,在 for 循环中:

for(int x = binaryLen - 1; x >= 0; x--)
{
    bitVal = binary[x];
    preVal = bitVal * base;
    totVal += preVal;
    base = base * 2;
}
4

2 回答 2

1

你有正确的想法decode。我不明白为什么您的代码不起作用,尽管我在pow任何地方都看不到实现。

将十进制转换为 ascii 很容易,只需将值转换为 char:

int v = ...;       // decimal value
char c = (char)v;  // ascii value
于 2013-02-27T03:06:45.843 回答
0
    int[] bValue = {1,0,0,0,1,0,1};

    int iValue = 0;
    // convert binary to decimal
    for (int i = 0, pow = bValue.length - 1 ; i < bValue.length ; i++, pow--)
        iValue += bValue[i]*Math.pow(2, pow);

    // gets the value as a char
    char cValue = (char)iValue;

    System.out.println("Int value: "+iValue +", Char value : "+cValue);

如果您需要整个 ASCII 表,您可以将值放入 M​​ap 中,其中键是整数值,值是对应的 ASCII 条目。

于 2013-02-27T03:19:48.097 回答