bytes_to_int bug?

new topic     » topic index » view thread      » older message » newer message

I recently needed to read some binary integers from a file, so
I used the routine:

   function read_16bit( handle )

      integer b1, b2

      b1 = getc( handle )
      b2 = getc( handle )

      return (b2*256) + b1

   end function

This worked fine most of the time, on most files, but in some cases
would return something like -254, which was clearly not right. This
was because it was reading two #FFFFs, which it read as -1. So I
changed the code to:

   function read_16bit( handle )

      integer b1, b2

      b1 = getc( handle )
      b2 = getc( handle )

      return bytes_to_int( { b1, b2, 0, 0 } )

   end function

But this still did not work. Finally, I changed it to:

   function read_16bit( handle )

      integer b1, b2

      b1 = getc( handle )
      b2 = getc( handle )

      return and_bits(b2*256, b1)

   end function

Which returned a reasonable result.

My question: is there a bug in bytes_to_int (and, by
implication, ints_to_byte)?

-- David

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu