I'm trying to make a piece of code that converts a byte variable type into a readable binary value.
CODE
import java.io.*;
import java.util.Scanner;
/**
* A program that hides messages in images.
* @author Mike Magnus 497020
*
*/
public class steganography
{
public static void main(String[] args)
{
byte smallChar = 0;
char largeChar = 0;
String user = "The End is Near";
String binary = "";
Scanner input = new Scanner(System.in);
largeChar = user.charAt(0);
System.out.print((int)largeChar);
smallChar = (byte) largeChar;
System.out.print(smallChar + "\n");
for(int cnt = 0;cnt < 8; cnt++)
{
if(((smallChar / 128) >= 1) || ((smallChar / 128) <= -1))
binary += "1";
else
binary += "0";
smallChar = (byte) (smallChar << 1);
System.out.print((byte)smallChar + "\n");
}
System.out.print(binary);
}
}
I just have a few questions about the byte type.
1) Why does it keep jumping between positive and negative?
2) Why does assigning it a value not assign that value (ie assigning 130 will give it -126)
I know the code is pretty sloppy right now, it was nicer but I was getting frustrated with this and started butchering my code.