From: Harry Kakueki Date: 2012-10-17T14:08:34+09:00 Subject: Re: Converting binary string to integer for a Ruby newbie On Wed, Oct 17, 2012 at 3:16 AM, John Ringwalt wrote: > I want to convert a string of binary code ("011001") to the integer > value WITHOUT using to_i or any other preset method. How can I do this? > You can try this. n = "0011001" t = 0 n.each_char{|x| t = t*2+x.to_i} p t But if you are against using #to_i even like this, you could try this.. t = 0 n.each_char{|x| t = x=="1" ? t*2+1 : t*2} p t Or maybe, t=0 n.each_char{|x| t=t*2+(x<=>"0")} p t Harry