0

This is a homework assignment that I can't wrap my head around. I have to do it manually, so I can't use "getBytes()." Also, I have to convert to decimal format first, and then convert the decimal to ASCII (e.g. {0,1,1,0,0,0,1,0} = 98 in decimal format, which is a 'b'). I have arranged the binary code into an array, and want to use a for loop to traverse the array position by position. However, I'm not sure I'm using the correct parameters for the for loop, and am not sure how to divide the code into bits of "8." Another thing, how to I convert the decimal value to ASCII? Should I just list out all the possible letters I know I will get, and then refer to them using an if-else loop? Or could I just convert the decimal to a char? Here is my code so far... (It's a bit messy, sorry)

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);        
    }
}

EDIT:

Okay, so I figured out this much, under the class Decoder, in the decode block, in the for loop:

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

2 Answers2

1

You've got the right idea for decode. I don't see why your code wouldn't work, although I don't see the pow implementation anywhere.

Decimal to ascii is easy, just cast the value to a char:

int v = ...;       // decimal value
char c = (char)v;  // ascii value
Keith Randall
  • 22,985
  • 2
  • 35
  • 54
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);

If you need the whole ASCII table, you may put the values into a Map where the key is the integer value, and the value is the corresponding ASCII entry.