From: Matthew Miller Date: 2002-10-17T07:45:00+09:00 Subject: Re: Porting a CRC function to ruby Hi Bill, In article , William Djaja Tjokroaminata wrote: > Hi Matthew, > > The problem is not signed vs unsigned. The problem is, given an int, the > Ruby Fixnum is obtained by left shifting the int by one (and setting the > least significant bit to 1). Therefore, before you do your CRC, you have > to right shift the Fixnum. (Just be careful that the right shifted > version of the Fixnum may not be a valid Ruby object, so "don't interact > with Ruby" using the right shifted version.) > > The problem is probably more complicated if the data type used in the C > function is unsigned int or unsigned long. In this case probably you have > to use Bignum (and know how Bignum is implemented internally in > Ruby). However, as Ruby performs conversion between Bignum and Fixnum > automatically, you have to be careful too. > > Finally, because when you are dealing with communication packets the Ruby > Fixnum is not really "clean", probably it is better and easier to use Ruby > BitVector instead of Fixnum. Thanks for the insight. I tried my suggestion of masking off all but the lower two bytes and the CRC works with the packets I've tested it with. Still I wish that ruby had an unsigned integer class. BTW, why does Fixnum left shift by one the integer it stores? I can't think of any reason why that would be done. Thanks, Matthew -- "Man k�nnte froh sein, wenn die Luft so rein w�re wie das Bier" "One could be happy if the air were as pure as the beer"