Results 1 to 17 of 17
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default 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..

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: How to write bytes above 128?

    Cast the value to byte: (byte)200

  3. #3
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: How to write bytes above 128?

    If cast, how to know it is 200?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: How to write bytes above 128?

    Print it out

  5. #5
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: How to write bytes above 128?

    (byte)200 = (200 - 127) - (128 + 1) = -56

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default 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

  7. #7
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: How to write bytes above 128?

    Any value >127 will not be a negative number.
    (byte)300 = 44

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: How to write bytes above 128?

    a byte has 8 bits. The max unsigned value it can hold is 255

  9. #9
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default 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?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default 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.

  11. #11
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default 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.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default 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.

  13. #13
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default 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.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: How to write bytes above 128?

    2 dimension byte array can represent max value
    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?

  16. #16
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: How to write bytes above 128?

    Quote Originally Posted by Norm View Post
    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?
    Question was is it possible to put value > 255 into signed byte.
    Like is it possible to put 20 people in 4 seats car. :)
    Of course int can be used, as a bus can be used.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: How to write bytes above 128?

    Question was is it possible to put value > 255 into signed byte.
    That is a very clear question after much wandering around. The answer is NO.

Similar Threads

  1. How to write bytes to text file?
    By fatabass in forum New To Java
    Replies: 10
    Last Post: 09-18-2012, 07:46 PM
  2. Computing bytes
    By captainjack in forum New To Java
    Replies: 5
    Last Post: 10-28-2011, 01:22 AM
  3. How to read first 9 bytes and write in...?
    By aRTx in forum New To Java
    Replies: 8
    Last Post: 03-26-2009, 02:54 PM
  4. DES algorithm (Read and Write bytes to file)
    By JoaoPe in forum Advanced Java
    Replies: 6
    Last Post: 07-29-2008, 03:46 PM
  5. how to know the number of bytes
    By gabriel in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 05:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •