wood burning stoves*
The moose likes Beginning Java and the fly likes how to store 255 in byte Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to store 255 in byte" Watch "how to store 255 in byte" New topic
Author

how to store 255 in byte

sagar kumar nerella
Greenhorn

Joined: Jul 09, 2011
Posts: 18

my main problem is how to store the value 255 in a byte in java.

in my theme :
i am making a reader and writer for a bmp image . so i am unable to read the unsigned byte values(means to read & write value >128) in single byte only.

i.e like i want to store byte b=255; in any way and this value should be written to the image.
my problem of reading was solved by readUsnsignedByte()

but problem of how to write this value to the file.as 255 in single byte only is not addressed

(in brief i want to set the gray level of a pixel in bmp to 255 using java and read the same as well using only one byte)
so please any one can help me


sagar kumar nerella,
java certified professional
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3598
    
  14

Just take an int, set it to 255 and then cast it to a byte. The cast will lop off the three high order bytes, leaving you with the byte value -1 (which is the same as 255 unsigned).
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18529
    
  40

Stephan van Hulst wrote:Just take an int, set it to 255 and then cast it to a byte. The cast will lop off the three high order bytes, leaving you with the byte value -1 (which is the same as 255 unsigned).


If you like, you can also do it without an intermediate variable.



The compiler will know what you mean, and calculate the int value of 255 casted to a byte, and do it at compile time.... ie. no three higher order bytes to lop off.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
To turn it back, however, you need a mask.
sagar kumar nerella
Greenhorn

Joined: Jul 09, 2011
Posts: 18

ok thanks to all

but my problem is i need to write it to the byte stream( a bmp file as a pixel value) and then retrieve this value.

which i did in 'c' easily.

if any body can give good book on image processing in java it will be more helpful to my concept
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
There is a package which might be helpful. I am not sure; I can’t get the pages to open properly. Maybe you will have better luck if you try later.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to store 255 in byte
 
Similar Threads
How to read/write directly to/from a class???
covert from byte to string and back to byte
Getting information from a Image file stored on client side
Ragged matrix and histogram image problem
Combining two files to create a new file and then reading it...