For Programmers: Free Programming Magazines  


Home > Archive > Java Help > May 2004 > reading/writing a byte array in java









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author reading/writing a byte array in java
Drew

2004-04-12, 10:42 am


Hi Gang:

byte[] bytes;


I have a byte array as declared above that has been filled with the
data from a .jpg file. Let's say that I wish to find the index of the
fifth occurrence of the hex char 0xff from the start of the byte
array.

What is wrong with the following? I know its got to be the way I am
comparing in the if statement. This works in C but I guess I need
something different in Java?

Thanks!
Drew



for (x = 0, ffcounter = 0; x < bytes.length; x++)
{
if (bytes[x] == 0xff) // counter for ff values
ffcounter++;


// on the 5th one, exit for loop so
// that x has index of 5th ff

if (ffcounter == 5)
break;
}

// index of the 5th occurrence of 0xff is value of 'x'




Roedy Green

2004-04-12, 3:33 pm

On Mon, 12 Apr 2004 09:51:29 -0400, Drew <drew@drew.com> wrote or
quoted :

> for (x = 0, ffcounter = 0; x < bytes.length; x++)
> {
> if (bytes[x] == 0xff) // counter for ff values
> ffcounter++;
>
>
>// on the 5th one, exit for loop so
>// that x has index of 5th ff
>
> if (ffcounter == 5)
> break;
> }


Several problems for me.

1. the canonical loop counter goes for ( int i=0; i<bytes.length; i++)
so i is not known outside the loop. It confuses people when you
violate canonical forms. If you want to know i outside the loop, store
it in a separate variable to make it very clear what you are doing and
make i known only inside the loop. C people will likely disagree with
me, Algol people will likely agree. It is a matter of creating least
surprise.

2. The for loop has a built in stopping mechanism i<bytes.length. Use
that. It is where people expect to find the stopping criteria. You
might consider ffcounter < 5 && i < bytes.length

3. Fortran background people would never use x as a loop counter. It
is reserved as a float or double temporary.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Mark Hansen

2004-04-12, 3:33 pm

On 4/12/2004 11:47, Roedy Green wrote:

> On Mon, 12 Apr 2004 09:51:29 -0400, Drew <drew@drew.com> wrote or
> quoted :
>
>
> Several problems for me.
>
> 1. the canonical loop counter goes for ( int i=0; i<bytes.length; i++)
> so i is not known outside the loop. It confuses people when you
> violate canonical forms. If you want to know i outside the loop, store
> it in a separate variable to make it very clear what you are doing and
> make i known only inside the loop. C people will likely disagree with
> me, Algol people will likely agree. It is a matter of creating least
> surprise.


Did I miss something? The original posting did not create the variable
inside the for loop.


>
> 2. The for loop has a built in stopping mechanism i<bytes.length. Use
> that. It is where people expect to find the stopping criteria. You
> might consider ffcounter < 5 && i < bytes.length


.... unless there is a "break" statement. This is actually quite
common usage.

>
> 3. Fortran background people would never use x as a loop counter. It
> is reserved as a float or double temporary.


But that doesn't really affect the outcome of the program, does it?

>
> --
> Canadian Mind Products, Roedy Green.
> Coaching, problem solving, economical contract programming.
> See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Eric Sosman

2004-04-12, 4:33 pm

Drew wrote:
>
> Hi Gang:
>
> byte[] bytes;
>
> I have a byte array as declared above that has been filled with the
> data from a .jpg file. Let's say that I wish to find the index of the
> fifth occurrence of the hex char 0xff from the start of the byte
> array.
>
> What is wrong with the following? I know its got to be the way I am
> comparing in the if statement. This works in C but I guess I need
> something different in Java?
>
> Thanks!
> Drew
>
> for (x = 0, ffcounter = 0; x < bytes.length; x++)
> {
> if (bytes[x] == 0xff) // counter for ff values


A `byte' has a value in the range -128..+127. You will
notice that 0xFF is 255, which is not in that range. Hence,
no `byte' value can ever satisfy this `if' statement.

Two possible fixes:

if (bytes[x] == -1) ...

if ((bytes[x] & 0xFF) == 0xFF) ...

--
Eric.Sosman@sun.com
Roedy Green

2004-04-12, 7:17 pm

On Mon, 12 Apr 2004 11:53:56 -0700, Mark Hansen <meh@NOSPAMunify.com>
wrote or quoted :

>Did I miss something? The original posting did not create the variable
>inside the for loop.


I know. It is legit C and Java, but illegitimate in other languages to
use a loop counter outside the loop. It is not the canonical form of a
loop. I think it should be avoided if possible. It is a weird thing
to do, it deserves special note by storing the loop counter is a
separate variable where you exit the loop prematurely.

You just confuse the heck out of people by writing code where loop
counters live outside the loop. You need to red flag it in some way.

His code with a comment would suffice.






--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Roedy Green

2004-04-12, 7:17 pm

On Mon, 12 Apr 2004 11:53:56 -0700, Mark Hansen <meh@NOSPAMunify.com>
wrote or quoted :

>
>
>But that doesn't really affect the outcome of the program, does it?


No, but it makes you program more confusing since in violates a long
standing tradition that other programmers expect. i,j,k are ints.
x,y,z are floating point.

Similarly you make packages start with lower case, variable lower
case, and classes upper case. Code still works otherwise, but it makes
the code very confusing to someone used to trusting the convention.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Roedy Green

2004-04-12, 7:17 pm

On Mon, 12 Apr 2004 14:58:06 -0400, Eric Sosman <Eric.Sosman@sun.com>
wrote or quoted :

>if ((bytes[x] & 0xFF) == 0xFF) ...


for more details see http://mindprod.com/jgloss/unsigned.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Mark Hansen

2004-04-12, 7:17 pm

On 4/12/2004 15:10, Roedy Green wrote:

> On Mon, 12 Apr 2004 11:53:56 -0700, Mark Hansen <meh@NOSPAMunify.com>
> wrote or quoted :
>
>
> No, but it makes you program more confusing since in violates a long
> standing tradition that other programmers expect. i,j,k are ints.
> x,y,z are floating point.


To you perhaps. However, we're writing Java. In java, there is no
such tradition.

>
> Similarly you make packages start with lower case, variable lower
> case, and classes upper case. Code still works otherwise, but it makes
> the code very confusing to someone used to trusting the convention.
>
> --
> Canadian Mind Products, Roedy Green.
> Coaching, problem solving, economical contract programming.
> See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Christophe Vanfleteren

2004-04-12, 7:55 pm

Mark Hansen wrote:

>
> To you perhaps. However, we're writing Java. In java, there is no
> such tradition.
>


I'm pretty sure that 99% of all loops out there are using i/j/k as a counter
variable.


--
Kind regards,
Christophe Vanfleteren
Drew

2004-04-13, 11:49 am


Hi Roedy:

Thanks for the thoughts. Really, the problem I'm having is why the
logic of the if statement doesn't work. Perhaps there is room for
improvement in the style of the code fragment, but that is not the
issue here.

Thanks
Drew

On Mon, 12 Apr 2004 18:47:23 GMT, Roedy Green
<look-at-the-website@mindprod.com> wrote:

>On Mon, 12 Apr 2004 09:51:29 -0400, Drew <drew@drew.com> wrote or
>quoted :
>
>
>Several problems for me.
>
>1. the canonical loop counter goes for ( int i=0; i<bytes.length; i++)
>so i is not known outside the loop. It confuses people when you
>violate canonical forms. If you want to know i outside the loop, store
>it in a separate variable to make it very clear what you are doing and
>make i known only inside the loop. C people will likely disagree with
>me, Algol people will likely agree. It is a matter of creating least
>surprise.
>
>2. The for loop has a built in stopping mechanism i<bytes.length. Use
>that. It is where people expect to find the stopping criteria. You
>might consider ffcounter < 5 && i < bytes.length
>
>3. Fortran background people would never use x as a loop counter. It
>is reserved as a float or double temporary.


Drew

2004-04-13, 11:49 am


Eric:

Just wanted to thank you for answering my question. You nailed it! I
should have realized that! I guess sometimes you just stare at
something so long you can't see the obvious. Thanks for the help.
THe -1 solution works perfectly!

Drew

On Mon, 12 Apr 2004 14:58:06 -0400, Eric Sosman <Eric.Sosman@sun.com>
wrote:

>Drew wrote:
>
> A `byte' has a value in the range -128..+127. You will
>notice that 0xFF is 255, which is not in that range. Hence,
>no `byte' value can ever satisfy this `if' statement.
>
> Two possible fixes:
>
> if (bytes[x] == -1) ...
>
> if ((bytes[x] & 0xFF) == 0xFF) ...


Knute Johnson

2004-04-13, 8:32 pm

Eric Sosman wrote:
> Drew wrote:
>
>
>
> A `byte' has a value in the range -128..+127. You will
> notice that 0xFF is 255, which is not in that range. Hence,
> no `byte' value can ever satisfy this `if' statement.
>
> Two possible fixes:
>
> if (bytes[x] == -1) ...
>
> if ((bytes[x] & 0xFF) == 0xFF) ...
>


Or even more readable: if (bytes[x] == (byte)0xff))

--

Knute Johnson
email s/nospam/knute/
Molon labe...
Ben Aroia

2004-04-30, 1:22 pm

Knute Johnson <nospam@sagebrush.frazmtn.com> wrote in message news:<8b23824f9ce6cce1009d1f123b6e42a2@news.1usenet.com>...
> Eric Sosman wrote:
>
> Or even more readable: if (bytes[x] == (byte)0xff))

i thought that a byte had a value from -255 to 255. i might be wrong
if the code doesn't work.
ben
Christophe Vanfleteren

2004-04-30, 1:22 pm

Ben Aroia wrote:
>
> i thought that a byte had a value from -255 to 255. i might be wrong
> if the code doesn't work.


No it doesn't. Since a byte (by eight) consists of 8 bits, you can only use
the range from 00000000 to 11111111.

When using signed bytes (the only ones available in Java), you lose 1 digit
for the sign, so there are only seven left for the "value" itself.

With those 7, you can make 128 (2^7) combinations. So that gives us a range
from -128 (-2^7) to 127 (2^7-1), when using the first bit as sign bit.

when you don't use a sign bit, you can use all of the 8 bits, so that gives
you 2^8 possible values, which is the range from 0 to 256.

--
Kind regards,
Christophe Vanfleteren
Roedy Green

2004-05-04, 4:01 pm

On Fri, 30 Apr 2004 12:31:57 GMT, Christophe Vanfleteren
<c.v4nfl3t3r3n@pandora.be> wrote or quoted :

>When using signed bytes (the only ones available in Java), you lose 1 digit
>for the sign, so there are only seven left for the "value" itself.


You don't have to lose it. See
http://mindprod.com/jgloss/unsigned.html
for how to get it back as a unsigned data bit.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com