Check for 0xff in byte array

Discussion in 'Java' started by Steve Rainbird, Jul 16, 2008.

  1. I need to check if there is a 0xff in a particular byte of a byte array
    (actually in 2 consecutive bytes) which represents a trailer of a file.

    I can't work out how to do it.

    Any help would be appreciated.

    TIA

    --
    Steve
     
    Steve Rainbird, Jul 16, 2008
    #1
    1. Advertisements

  2. Steve Rainbird wrote:
    > I need to check if there is a 0xff in a particular byte of a byte array
    > (actually in 2 consecutive bytes) which represents a trailer of a file.
    >
    > I can't work out how to do it.
    >
    > Any help would be appreciated.
    >
    > TIA
    >


    assume byte[] b

    if (b[n] == (byte)0xff)
    // true

    --

    Knute Johnson
    email s/nospam/knute2008/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
     
    Knute Johnson, Jul 16, 2008
    #2
    1. Advertisements

  3. Steve Rainbird

    Mark Space Guest

    Knute Johnson wrote:
    > Steve Rainbird wrote:
    >> I need to check if there is a 0xff in a particular byte of a byte
    >> array (actually in 2 consecutive bytes) which represents a trailer of
    >> a file.
    >>
    >> I can't work out how to do it.
    >>
    >> Any help would be appreciated.
    >>
    >> TIA
    >>

    >
    > assume byte[] b
    >
    > if (b[n] == (byte)0xff)
    > // true
    >


    Another possibility might be:

    public final static byte TRAILER = -1; // 0xff

    //...

    if( b == TRAILER ) {
    // true
    }

    Which I think avoids the conversion to int, which can be expensive.
     
    Mark Space, Jul 17, 2008
    #3
  4. Steve Rainbird

    Mark Space Guest

    Peter Duniho wrote:
    > On Wed, 16 Jul 2008 17:11:23 -0700, Mark Space
    > <> wrote:
    >
    >> [...]
    >> public final static byte TRAILER = -1; // 0xff
    >>
    >> //...
    >>
    >> if( b == TRAILER ) {
    >> // true
    >> }
    >>
    >> Which I think avoids the conversion to int, which can be expensive.

    >
    > I'm in favor of using a constant simply because I don't like unnamed
    > magic numbers in my code. But I'm curious what conversion to int you
    > are saying might be avoided? If "b" is a byte[], then "b[n]" is a byte
    > and so no conversion should be necessary to compare "(byte)0xff" (which
    > is also a byte, at compile-time) to "b[n]".
    >
    > Pete


    First, your right, I feebed. I thought I read that Knute was casting to
    int, not byte. My bad.

    However, I've had problems with performance with bytes before, so I did
    some checking before posting this. I don't see any JVM byte code that
    compares bytes. Just ints, doubles, longs and references. So I think
    both Knutes and my version will convert both bytes to ints before doing
    the compare.

    Might as well do

    if( (int)b[n] == -1 )

    and get it over with. There's only one conversion there, not two.
    Though I think this might be a lot harder to read than the other two
    versions.
     
    Mark Space, Jul 17, 2008
    #4
  5. Mark Space wrote:
    > However, I've had problems with performance with bytes before, so I did
    > some checking before posting this. I don't see any JVM byte code that
    > compares bytes. Just ints, doubles, longs and references. So I think
    > both Knutes and my version will convert both bytes to ints before doing
    > the compare.


    It's been a while since I last played with the bytecode, but from what I
    recall, a byte-to-int conversion is free while an int-to-byte is not.

    Certainly, it goes without mention that the decision of making bytes
    signed was a poor one.
    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Jul 17, 2008
    #5
  6. Steve Rainbird

    Mark Space Guest

    Joshua Cranmer wrote:

    >
    > It's been a while since I last played with the bytecode, but from what I
    > recall, a byte-to-int conversion is free while an int-to-byte is not.



    That might be true. The calculations I was testing converted bytes to
    ints, then back again. So the second operation might have been what was
    causing the slower processing.
     
    Mark Space, Jul 17, 2008
    #6
  7. "Knute Johnson" <> wrote in message
    news:487e7733$0$4069$...
    > Steve Rainbird wrote:
    >> I need to check if there is a 0xff in a particular byte of a byte array
    >> (actually in 2 consecutive bytes) which represents a trailer of a file.
    >>
    >> I can't work out how to do it.
    >>
    >> Any help would be appreciated.
    >>
    >> TIA
    >>

    >
    > assume byte[] b
    >
    > if (b[n] == (byte)0xff)
    > // true
    >
    > --
    >
    > Knute Johnson
    > email s/nospam/knute2008/
    >
    > --
    > Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    > ------->>>>>>http://www.NewsDemon.com<<<<<<------
    > Unlimited Access, Anonymous Accounts, Uncensored Broadband Access



    Thanks everybody, as usual you have come up trumps.

    Thanks from a Cobol programmer dabbling in Java!!

    --
    Steve
     
    Steve Rainbird, Jul 17, 2008
    #7
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bharat Bhushan

    Appending byte[] to another byte[] array

    Bharat Bhushan, Aug 5, 2003, in forum: Java
    Replies:
    15
    Views:
    41,327
    Roedy Green
    Aug 5, 2003
  2. Kirby
    Replies:
    3
    Views:
    932
    Kirby
    Oct 8, 2004
  3. Replies:
    7
    Views:
    2,269
    espenskogen
    Jan 31, 2011
  4. Replies:
    20
    Views:
    10,523
    licebmi
    Sep 7, 2009
  5. Jorge Peixoto

    Why write putc(s.i16 & 0xff, fp);

    Jorge Peixoto, Oct 26, 2007, in forum: C Programming
    Replies:
    30
    Views:
    1,283
    Old Wolf
    Oct 28, 2007
  6. Tom McGlynn
    Replies:
    4
    Views:
    1,064
    Mark Space
    Apr 19, 2008
  7. Tom McGlynn
    Replies:
    2
    Views:
    516
    Andreas Leitgeb
    Apr 18, 2008
  8. Sylvester T Cat
    Replies:
    2
    Views:
    533
    Brian Candler
    Dec 12, 2010
Loading...