Hello everyone I am making a code to take a binary code and then embed it within the first row of a picture. Then with that another function takes it out of the picture and converts it into decimal. The problem is that I am pretty sure I did it the way my teacher explained to convert and I did it by hand to see if the program was any different...it was not...by hand and the program got 22 166 54 54 246 ...but that doesn't see to make sense... I want to know if my math in the code and by hand is wrong or just my understanding of converting...Thanks

also aside from the problem...I am open for advice on improving my code.

/**
 * This program codes a message and then decodes it
 * 
 * @author Troyle Thomas
 * @version 8/20/10
 */
import java.awt.*;
class Message
{
    public void encodeMessage(Picture earth, int [] messageArray)
    {
        Pixel pixelTarget = new Pixel(earth,0,0);
        Pixel [] pixelArray = earth.getPixels();
        Color pixelColor = null;
        int redValue = 0;
        
        System.out.println(" ");
        System.out.println("Binary Code");

        for(int x = 0; x < messageArray.length; x++)
        {
            redValue = messageArray[x];  //takes message and assigns to redvalue one by one
            pixelTarget = pixelArray[x]; 
            pixelTarget.setRed(redValue);
            
            System.out.print(messageArray[x] + " ");
        }
        System.out.println(" ");
        pixelTarget = pixelArray[messageArray.length];
        pixelTarget.setRed(255);
        
        earth.write("SecretMessage.bmp");
        earth.explore();
        
    }
 
    public void decodeMessage(Picture earth, int [] messageArray)
    {
       Pixel pointPixel = new Pixel(earth,0,0);
       Pixel [] pixelList = earth.getPixels();
       int [] messageBin = new int[messageArray.length];
       int [] rawAscii = new int[messageArray.length];
       int q = 0;
       
       System.out.println(" ");
       System.out.println("ASCII");
       for(int a = 0; a < messageArray.length; a++)
       {
            pointPixel = pixelList[a];
            messageBin[a] = pointPixel.getRed();
            
            if(q == 8)
                q = 0;
            rawAscii[a] = messageBin[a] * (int)(Math.pow(2.0,(double)(q)));
            q++;
            
           
       }
       
       
       int [] asciiSymbol = new int[rawAscii.length/8];
       int add = 0;
       for (int b = 0; b < rawAscii.length/8; b++)
       {
           asciiSymbol[b] = rawAscii[0 + add] + rawAscii[1 + add] + rawAscii[2 + add] + rawAscii[3 + add] + rawAscii[4 + add] + rawAscii[5 + add] + rawAscii[6 + add] + rawAscii[7 + add];
           add = add + 8; 
           
           System.out.print(asciiSymbol[b] + " "); 
       }
    }
}
public class HideMessageTester
{ 
    public static void main(String[] args)
    {
        //int[] messageArray = {0,1,1,0,0,0,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,1,1,0,0,1,1,0,0,0,0,1,0,1,1,1,0,0,1,0,0,1,1,1,1,0,0,1};
        int[] messageArray = {0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1,0,1,1,0,1,1,0,0,0,1,1,0,1,1,0,0,0,1,1,0,1,1,1,1};
        Picture earth = new Picture("Earth.bmp");
        Message hid = new Message();
            
        hid.encodeMessage(earth, messageArray);
        hid.decodeMessage(earth, messageArray);
    }
}

I want to know if my math in the code and by hand is wrong

Can you explain what your algorithm is and what you are trying to do?

The title for this thread is Binary to Decimal.
What format is used to represent these values?
Are they both Strings?
Everything in the computer is binary. You can look at the same byte or groups of bytes as either binary, decimal, float, character, machine instruction and others depending on what software/hardware is using the values.
For example a byte containing: 0100 1000 in binary is 0x48 and '0' depending on how you want to look at it. The same value in memory looked at 3 ways.

This program was an assignment that I was told to do. I am just not sure I understood the explanation of converting binary to decimal correctly.

First I had to use an online source to convert text to binary code. Then program takes a int array and I assign the binary code to that, which is in the main function. Then another function takes the binary code and embeds it into the red value of the first row of the picture. To distinguish where the binary code end on the first row, the next pixel's red value is set to 255. This is suppose to be a way to make a "secret message". Then another function extracts the secret message from the first row and then converts that into decimal. The output that is displays for this program is the extracted binary code and the the decimal. Now that we know the decimal, we can manually reference the ascii chart on our own.

You need a lot more details on the algorithm to code it.
First what is the binary code?
How is the binary code embedded in the picture?
How is the binary code converted to decimal?

Here is a sample code for converting a String into an array of int where each int represents a bit in the String:

// Create an array of int each element being a bit in the string
      String msg = "hello";
      int[] msgBits = new int[msg.length()*8];
      int idx = 0; // index to msgBits
      for(int i=0; i < msg.length(); i++) {
         char c = msg.charAt(i);
         for(int shift = 7; shift >= 0; shift--) {
            msgBits[idx++] = (c >> shift) & 1;
         } // end for(shift)
      } // end for(i)
      System.out.println("msgBits=" + Arrays.toString(msgBits));

Your code must undo what this code does. Take an array of int and recreate the byte.

Looking at your code there seems to be an asymmetry between the encode and the decode.
The encode stores an 8 bit value from an int into the red pixel. There's no messing about with individual bits.
The decode gets an 8 bit value from the red pixel, but then starts doing all kinds of bit shifting operations on it.
To make it symmetical all you need to do in the decode is to assign the red pixel value to the corresponding int array element - ie the exact opposite of the encode.
The messageArray could contain any values 0-255, not just 1 or 0. Maybe this is where the confusion crept in?

Edited 6 Years Ago by JamesCherrill: n/a

I think the OPs messageArray only contains bit values, not bytes. Another design might have it contain nibbles(half bytes) with values 0-15.

I left off the output from the above code:
msgBits=[0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1]

Edited 6 Years Ago by NormR1: n/a

Indeed that may be the case, in which the encode logic needs to unpack the bits. Either way, the encode & decode need to be symmetric. Reading the OPs spec, I can't dee any definition of how many bits are stored in each int, although the test case may be a clue, or may be a red herring.

This article has been dead for over six months. Start a new discussion instead.