Results 1 to 17 of 17
Thread: How to write bytes above 128?
- 01-19-2012, 10:40 PM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
How to write bytes above 128?
Regarding the method:
write(byte[], int, int) in for example FileOutputStream class..
byte values are between 0 - 255 where primitive type byte can store up to 128 in java.
This method gets a byte array as an argument.
How am I suppose to put the value 200 in a byte array so that I can write the byte value 200 to an FileOutputStream?
thanks..
- 01-20-2012, 12:01 AM #2
Re: How to write bytes above 128?
Cast the value to byte: (byte)200
- 01-20-2012, 01:21 AM #3
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: How to write bytes above 128?
If cast, how to know it is 200?
- 01-20-2012, 01:36 AM #4
Re: How to write bytes above 128?
Print it out
- 01-20-2012, 01:51 AM #5
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: How to write bytes above 128?
(byte)200 = (200 - 127) - (128 + 1) = -56
- 01-20-2012, 01:56 AM #6
Re: How to write bytes above 128?
What is the purpose of the expression you just posted?
The byte data type is signed. Any value > 127 will have the sign bit set and be a negative number.The compiler promotes bytes to ints which spreads the sign bit. If you want an unsigned value from a byte AND it with 0xFF
- 01-20-2012, 02:16 AM #7
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: How to write bytes above 128?
Any value >127 will not be a negative number.
(byte)300 = 44
- 01-20-2012, 02:19 AM #8
Re: How to write bytes above 128?
a byte has 8 bits. The max unsigned value it can hold is 255
- 01-20-2012, 02:37 AM #9
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: How to write bytes above 128?
OK, and my question was what if array need to have negative values, and You insert number (byte)200, how to know it is 200?
- 01-20-2012, 02:40 AM #10
Re: How to write bytes above 128?
As the saying goes, You can not have your cake and eat it too.
You can either treat the byte array as containing unsigned bytes with values from 0 to 255
or it can contain signed bytes with values from -128 to 127.
But not both.
- 01-20-2012, 03:05 AM #11
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: How to write bytes above 128?
Say there is array of byte, length 5 rows x 2 columns
new byte[5][2]
and say byte[0][1] = (int)(200 / 127) which is flag 1, that indicate value byte[0][0] is actually 256 + byte[0][0], or 256 + (-56) = 200
Or something like that?Last edited by diamonddragon; 01-20-2012 at 03:08 AM.
- 01-20-2012, 01:14 PM #12
Re: How to write bytes above 128?
Not sure I understand what you are trying to say.
Each element in a byte array is a byte.
- 01-20-2012, 03:38 PM #13
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: How to write bytes above 128?
This idea is about 2 dimension byte array can represent max value 27 * 28 - 1 = 32767, using flag as above.
- 01-20-2012, 04:44 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Re: How to write bytes above 128?
You essentially don't write positive or negative byte values to a stream, you write eight bit patterns instead; it's only how you interpret those patterns, e.g. if you write the pattern 11111111 you may interpret the pattern as -1 or as 255 but the stream doesn't care ... the Java language compiler may protest but a simple cast to a byte type will make it keep its mouth shut.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-20-2012, 05:32 PM #15
Re: How to write bytes above 128?
Why the 2 dim array? The array holds 4 bytes, with 4 bits used as the sign. Why not use an int with only 1 bit used for sign?2 dimension byte array can represent max value
- 01-20-2012, 08:40 PM #16
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
- 01-20-2012, 10:37 PM #17
Similar Threads
-
How to write bytes to text file?
By fatabass in forum New To JavaReplies: 10Last Post: 09-18-2012, 07:46 PM -
Computing bytes
By captainjack in forum New To JavaReplies: 5Last Post: 10-28-2011, 01:22 AM -
How to read first 9 bytes and write in...?
By aRTx in forum New To JavaReplies: 8Last Post: 03-26-2009, 02:54 PM -
DES algorithm (Read and Write bytes to file)
By JoaoPe in forum Advanced JavaReplies: 6Last Post: 07-29-2008, 03:46 PM -
how to know the number of bytes
By gabriel in forum New To JavaReplies: 2Last Post: 08-06-2007, 05:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks