[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ruby / php operator differences.

warhero

3/8/2007 4:37:00 AM

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);
b = in.readUnsignedByte();
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:
def read_int
n = 0;
b = @input_stream.read_special(1,'C')
result = 0

while((b & 0x80) != 0 && n < 3)
result <<= 7
result |= (b & 0x7f)
b = @input_stream.read_special(1,'C')
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-....

2 Answers

Brian Candler

3/8/2007 7:07:00 AM

0

On Thu, Mar 08, 2007 at 01:36:38PM +0900, Aaron Smith wrote:
> 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

Errm, how about providing some evidence for that claim?

irb(main):010:0> 5 | 3
=> 7
irb(main):011:0> i = 0
=> 0
irb(main):012:0> i |= 0xe000000
=> 234881024

warhero

3/8/2007 7:12:00 AM

0

i wasn't 'claiming' anything. I was asking about why that was happening.
I figured it out though.


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