这是一项家庭作业,我无法解决这个问题。我必须手动完成,所以我不能使用“getBytes()”。另外,我必须首先转换为十进制格式,然后将十进制转换为ASCII(例如{0,1,1,0,0,0,0,0} = 98十进制格式,这是'b') 。我已经将二进制代码排列成一个数组,并希望使用for循环来按位移动数组位置。但是,我不确定我是否正在使用for循环的正确参数,并且不确定如何将代码分成“8”的位。另一件事,如何将十进制值转换为ASCII?我应该列出我知道的所有可能的字母,然后使用if-else循环来引用它们吗?或者我可以将小数转换为char吗?到目前为止,这是我的代码......(这有点乱,对不起)
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;
}
答案 0 :(得分:0)
你对decode
有了正确的想法。我不明白为什么你的代码不起作用,虽然我没有在任何地方看到pow
实现。
十进制ascii很简单,只需将值转换为char:
int v = ...; // decimal value
char c = (char)v; // ascii value
答案 1 :(得分: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表,可以将值放入Map中,其中键是整数值,值是相应的ASCII条目。