Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,615 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 971 people online right now. Registration is fast and FREE... Join Now!




Having trouble with byte variables

 
Reply to this topicStart new topic

Having trouble with byte variables

TriggaMike
post 11 Oct, 2008 - 02:37 PM
Post #1


New D.I.C Head

*
Joined: 26 Sep, 2008
Posts: 40



Thanked 3 times
My Contributions


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.

User is offlineProfile CardPM

Go to the top of the page

baavgai
post 11 Oct, 2008 - 03:19 PM
Post #2


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,967



Thanked 96 times

Dream Kudos: 475

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


I'm afraid bytes are signed in java and there's no unsigned variety. So, for byte, the range is -128..127.

You do a simple test to confirm this yourself. This:
java

for(int value : new int[] {-129, -128, 127, 128}) {
System.out.println(value + " = " + (byte)value);
}


Results:
CODE

-129 = 127
-128 = -128
127 = 127
128 = -128


You could just use an int.
User is offlineProfile CardPM

Go to the top of the page

William_Wilson
post 11 Oct, 2008 - 03:23 PM
Post #3


lost in compilation

Group Icon
Joined: 23 Dec, 2005
Posts: 3,970



Thanked 15 times

Dream Kudos: 3275

Expert In: Java, C, Javascript

My Contributions


with a function along the lines of: http://www.dreamincode.net/code/snippet1001.htm ?


EDIT: haha, I just noticed that the snippet number is 1001 smile.gif

This post has been edited by William_Wilson: 11 Oct, 2008 - 03:23 PM
User is offlineProfile CardPM

Go to the top of the page

TriggaMike
post 11 Oct, 2008 - 05:16 PM
Post #4


New D.I.C Head

*
Joined: 26 Sep, 2008
Posts: 40



Thanked 3 times
My Contributions


Thank you so much. Here I was thinking bit was 0-255 when it's definitely not. Thanks guys
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 02:55AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month