In article <a5en6i$2hdt$1 / norfair.nerim.net>, Simon ARNAUD wrote:
> You are looking for the oct method.
> 
> "0b1110".oct => 14
> 
> I'm not sure the method was meant to work with bin, but it does.
> However, I didn't managed to find a solution without using a temp var
> a = "0b" + binstr
> num = a.oct
> 
> If someone knows a way, please post it

irb(main):012:0> binstr = '1110'
"1110"
irb(main):013:0> "0b#{binstr}".oct
14

maybe...  If you really want to make your ruby look like perl then

def BinStrToNum ( binStr )
    sum = 0
    [binStr.reverse].pack('b*').reverse.unpack('c*').each { |byte|
        sum <<= 8
        sum += byte.to_i
    }

    sum
end

 ;-)

Mike

>> How can I convert a string of binary digits to a number? I've got this
>> but I imagine there's some kind of built-in method. Is there?
>> 
>> def BinStrToNum ( binStr )
>>   sum = 0
>>   digit = 1
>>   binStr.reverse.each_byte { |i|
>>     sum += (i-48)*digit
>>     digit <<= 1
>>   }
>>   return sum
>> end
>> 
>> Thanks,
>> Andrew


-- 
mike / stok.co.uk                    |           The "`Stok' disclaimers" apply.
http://www.stok.co.uk/~mike/       | GPG PGP Key      1024D/059913DA 
mike / starnix.com                   | Fingerprint      0570 71CD 6790 7C28 3D60
http://www.starnix.com/            |                  75D2 9EC4 C1C0 0599 13DA