File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Java Bytes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Bytes" Watch "Java Bytes" New topic
Author

Java Bytes

Richard West
Ranch Hand

Joined: Jan 07, 2005
Posts: 127
Hi everyone,

Consider the below program



Now from what i understand 1 byte is 8 bits so the maximum is all 1's (ie. 255).

The above first function ByteTest1 i cast the hexadecimal a byte as so



Now why do i have to cast it even when 0x9C is well below 255 but if i do not cast it the compiler complains saying that it is an integer.

The above second function ByteTest2 i did not cast the hexadecimal a byte as so



Now why is it that the compiler accepts the hexadecimal value 0x5A without any casting but for 0x9C the compiler emit errors saying it is an integer?

Am i missing something?

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
Your missing that Byte are signed : (8 bits, all 1 is -1 !)

So byte range is -128 -> 127


Regards,<br />Seb<br /> <br />SCJP 1.4
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
In other words, the maximum value you can store in a byte is 127, NOT 255.

Layne


Java API Documentation
The Java Tutorial
Richard West
Ranch Hand

Joined: Jan 07, 2005
Posts: 127
Hi everyone,

In that case let's say if i have the number -8 and +10.
How do i convert them to signed bytes respectively?

Richard West
[ October 14, 2005: Message edited by: West Richard ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Richard,

Please check your private messages.


[Jess in Action][AskingGoodQuestions]
Mark Van Tuyl
Ranch Hand

Joined: Mar 22, 2002
Posts: 60


<a href="http://www.catb.org/~esr/faqs/smart-questions.html" target="_blank" rel="nofollow">How To Ask Smart Questions</a>
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by West Richard:
Hi everyone,

In that case let's say if i have the number -8 and +10.
How do i convert them to signed bytes respectively?

Richard West

[ October 14, 2005: Message edited by: West Richard ]


Even better than the above, you can just do this:

This works because these numbers are in the range of valid values for bytes. Also, they are already *signed*.

Perhaps you meant "unsigned" instead. If so, assigning these values to variables is the same, assuming you are actually interested in the bit pattern of the value -8, for instance. Once you do the assignment, you just treat the variable as if its unsigned. You can do any bit-wise operations you wish, as long as you cast the result back to a byte. (The result of &, |, ^, >>, and << is promoted to an int for some reason.)

I hope this answers your question. If not, perhaps you can illustrate what you are trying to do with some more specific code.

Layne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Bytes