I am trying to do some parsing of binary data.

Here is the psuedo code that parses what i'm looking for:
private int readInteger() throws IOException {
int n = 0;
int b = in.readUnsignedByte();
int result = 0;

while ((b & 0x80) != 0 && n < 3) {
result <<= 7;
result |= (b & 0x7f);
n++;
}
if (n < 3) {
result <<= 7;
result |= b;
} else {
/* Use all 8 bits from the 4th byte */
result <<= 8;
result |= b;

/* Check if the integer should be negative */
if ((result & 0x10000000) != 0) {
/* and extend the sign bit */
result |= 0xe0000000;
}
}

return result;
}

Here is how I wrote it with Ruby:
n = 0;
result = 0

while((b & 0x80) != 0 && n < 3)
result <<= 7
result |= (b & 0x7f)
n = n + 1
end
if (n < 3)
result <<= 7
result |= b
else
# Use all 8 bits from the 4th byte
result <<= 8
result |= b

#Check if the integer should be negative
if ((result & 0x10000000) != 0)
# and extend the sign bit
result |= 0xe0000000
end
end

STDOUT.puts "READ INT: #{result}"
return result
end

When translating the Pseudo Code to PHP, it's verbatim except for
syntax. With ruby, I think i've found the problem with it not parsing
correclty. the |= operator. With PHP

\$i |= 0xe0000000; //= -536870912

Now with Ruby:
i |= 0xe000000 #= true

Any ideas on a solution?

thanks.

--
Posted via http://www.ruby-forum.com/.