Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2004

Tip: Looking for answers? Try searching our database.

reading/writing a byte array in java

Thread view: 
Drew - 12 Apr 2004 14:51 GMT
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 - 12 Apr 2004 19:47 GMT
>   for (x = 0, ffcounter = 0; x < bytes.length; x++)
>       {
[quoted text clipped - 7 lines]
>           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 - 12 Apr 2004 19:53 GMT
>>   for (x = 0, ffcounter = 0; x < bytes.length; x++)
>>       {
[quoted text clipped - 17 lines]
> 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.
Roedy Green - 12 Apr 2004 23:05 GMT
>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 - 12 Apr 2004 23:07 GMT
>... unless there is a "break" statement. This is actually quite
>common usage.

The for loop's duty is to stop when an incrementing count hit a value.
Break is for other sorts of premature exit.

Break is a sort of GOTO that should be avoided if possible.
 
You could write the code as a while loop, but the general principle is
to use the most specific tool with the most idiomatic code.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Roedy Green - 12 Apr 2004 23:10 GMT
>> 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?

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.
Mark Hansen - 12 Apr 2004 23:17 GMT
>>> 3. Fortran background people would never use x as a loop counter. It
>>> is reserved as a float or double temporary.
[quoted text clipped - 4 lines]
> 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
[quoted text clipped - 4 lines]
> Coaching, problem solving, economical contract programming.
> See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Christophe Vanfleteren - 13 Apr 2004 00:09 GMT
>> 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.

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

Signature

Kind regards,
Christophe Vanfleteren

Drew - 13 Apr 2004 15:24 GMT
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

>>   for (x = 0, ffcounter = 0; x < bytes.length; x++)
>>       {
[quoted text clipped - 24 lines]
>3. Fortran background people would never use x as a loop counter. It
>is reserved as a float or double temporary.
Eric Sosman - 12 Apr 2004 19:58 GMT
> Hi Gang:
>
[quoted text clipped - 15 lines]
>        {
>          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) ...

Signature

Eric.Sosman@sun.com

Roedy Green - 12 Apr 2004 23:11 GMT
>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.
Drew - 13 Apr 2004 15:28 GMT
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

>> Hi Gang:
>>
[quoted text clipped - 25 lines]
>
>    if ((bytes[x] & 0xFF) == 0xFF) ...
Knute Johnson - 14 Apr 2004 00:53 GMT
>>Hi Gang:
>>
[quoted text clipped - 25 lines]
>
>     if ((bytes[x] & 0xFF) == 0xFF) ...

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

Signature

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

Ben Aroia - 30 Apr 2004 13:15 GMT
> >>Hi Gang:
> >>
[quoted text clipped - 27 lines]
>
> 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 - 30 Apr 2004 13:31 GMT
>> 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.

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.

Signature

Kind regards,
Christophe Vanfleteren

Roedy Green - 30 Apr 2004 19:03 GMT
>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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.