Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to store 255 in byte

 
sagar kumar nerella
Greenhorn
Posts: 18
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stephan van Hulst
Bartender
Pie
Posts: 5912
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21214
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49451
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To turn it back, however, you need a mask.
 
sagar kumar nerella
Greenhorn
Posts: 18
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49451
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic