# Convert binary array to floating point decimal

**URL:** <https://rubytalk.org/t/convert-binary-array-to-floating-point-decimal/33402>\
**Category:** ruby-talk\
**Created:** [2 December 2006 19:00 UTC](https://rubytalk.org/t/convert-binary-array-to-floating-point-decimal/33402 "2006-12-02T19:00:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nebiru](https://avatars.discourse-cdn.com/v4/letter/n/f17d59/32.png) [@Nebiru](https://rubytalk.org/u/Nebiru)\
**Post date:** [2 December 2006 19:00 UTC](https://rubytalk.org/t/convert-binary-array-to-floating-point-decimal/33402/1 "2006-12-02T19:00:05Z")

</div>

new to ruby here, looking for a little help

I'm trying to convert some C# code to ruby, however I'm not getting the  
desired results  
The code is supposed to convert a binary string to a floating point  
number

public float binaryStringToFloat(int length)  
{  
&nbsp;&nbsp;&nbsp;float result = 0;  
&nbsp;&nbsp;&nbsp;int num = 0;  
&nbsp;&nbsp;&nbsp;// form the integer number from the binary string  
&nbsp;&nbsp;&nbsp;for (int i = 0; i \< length; i++)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num = num \<\< 1;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num = num + binaryArray[(length - i) - 1];  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;// scale to the floating point value  
&nbsp;&nbsp;&nbsp;result = (float)num;  
&nbsp;&nbsp;&nbsp;result = result / (1 \<\< binaryArray.length/2);  
&nbsp;&nbsp;&nbsp;return result;  
}

any help will be appreciated

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)\
**Post date:** [2 December 2006 19:51 UTC](https://rubytalk.org/t/convert-binary-array-to-floating-point-decimal/33402/2 "2006-12-02T19:51:37Z")

</div>

> new to ruby here, looking for a little help
> 
> I'm trying to convert some C# code to ruby, however I'm not getting the  
> desired results  
> The code is supposed to convert a binary string to a floating point  
> number
> 
> public float

(int length)

> {  
> &nbsp;&nbsp;&nbsp;float result = 0;  
> &nbsp;&nbsp;&nbsp;int num = 0;  
> &nbsp;&nbsp;&nbsp;// form the integer number from the binary string  
> &nbsp;&nbsp;&nbsp;for (int i = 0; i \< length; i++)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num = num \<\< 1;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num = num + binaryArray[(length - i) - 1];  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;// scale to the floating point value  
> &nbsp;&nbsp;&nbsp;result = (float)num;  
> &nbsp;&nbsp;&nbsp;result = result / (1 \<\< binaryArray.length/2);  
> &nbsp;&nbsp;&nbsp;return result;  
> }
> 
> any help will be appreciated

Ruby has a built in method for converting a binary string into an  
integer, so your code is simply

def binaryStringToFloat(str)  
&nbsp;&nbsp;str.to\_i(2) \* 1.0/str.length  
end

but if you want to translate your algorithm literally:

def binaryStringToFloat(str)  
&nbsp;&nbsp;num = 0  
&nbsp;&nbsp;# reverse the string, split it into characters, then iterate over  
the characters  
&nbsp;&nbsp;str.reverse.split(//).each {|digit|  
&nbsp;&nbsp;&nbsp;&nbsp;num = (num \<\< 1) + digit.to\_i # \<\< has lower precedence than +  
&nbsp;&nbsp;}

&nbsp;&nbsp;return num \* 1.0 / (1 \<\< str.length)  
end

also, the pattern

accumulator = start\_val  
collection.each {|element|  
&nbsp;&nbsp;accumulator = f(accumulator, element)  
}

is captured by the inject method, so we can replace the "each" loop with

num = str.reverse.split(//).inject(0) {|sum, digit| (sum \<\< 1) + digit .to\_i}

martin

> **···**
>
> On 12/3/06, Nebiru \<Nebiru@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Nebiru](https://avatars.discourse-cdn.com/v4/letter/n/f17d59/32.png) [@Nebiru](https://rubytalk.org/u/Nebiru)\
**Post date:** [2 December 2006 20:45 UTC](https://rubytalk.org/t/convert-binary-array-to-floating-point-decimal/33402/3 "2006-12-02T20:45:09Z")

</div>

Wow, so much learned in one post...thank you

Martin DeMello wrote:

> **···**
>
> > On 12/3/06, Nebiru \<Nebiru@gmail.com\> wrote:  
> > \> new to ruby here, looking for a little help  
> > \>  
> > \> I'm trying to convert some C# code to ruby, however I'm not getting the  
> > \> desired results  
> > \> The code is supposed to convert a binary string to a floating point  
> > \> number  
> > \>  
> > \> public float  
> > (int length)  
> > \> {  
> > \> float result = 0;  
> > \> int num = 0;  
> > \> // form the integer number from the binary string  
> > \> for (int i = 0; i \< length; i++)  
> > \> {  
> > \> num = num \<\< 1;  
> > \> num = num + binaryArray[(length - i) - 1];  
> > \> }  
> > \> // scale to the floating point value  
> > \> result = (float)num;  
> > \> result = result / (1 \<\< binaryArray.length/2);  
> > \> return result;  
> > \> }  
> > \>  
> > \> any help will be appreciated
> > 
> > Ruby has a built in method for converting a binary string into an  
> > integer, so your code is simply
> > 
> > def binaryStringToFloat(str)  
> > &nbsp;&nbsp;str.to\_i(2) \* 1.0/str.length  
> > end
> > 
> > but if you want to translate your algorithm literally:
> > 
> > def binaryStringToFloat(str)  
> > &nbsp;&nbsp;num = 0  
> > &nbsp;&nbsp;# reverse the string, split it into characters, then iterate over  
> > the characters  
> > &nbsp;&nbsp;str.reverse.split(//).each {|digit|  
> > &nbsp;&nbsp;&nbsp;&nbsp;num = (num \<\< 1) + digit.to\_i # \<\< has lower precedence than +  
> > &nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;return num \* 1.0 / (1 \<\< str.length)  
> > end
> > 
> > also, the pattern
> > 
> > accumulator = start\_val  
> > collection.each {|element|  
> > &nbsp;&nbsp;accumulator = f(accumulator, element)  
> > }
> > 
> > is captured by the inject method, so we can replace the "each" loop with
> > 
> > num = str.reverse.split(//).inject(0) {|sum, digit| (sum \<\< 1) + digit .to\_i}
> > 
> > martin

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)\
**Post date:** [2 December 2006 20:54 UTC](https://rubytalk.org/t/convert-binary-array-to-floating-point-decimal/33402/4 "2006-12-02T20:54:35Z")

</div>

that should be

str.to\_i(2) \* 1.0/(1 \<\< str.length)

of course

martin

> **···**
>
> On 12/3/06, Martin DeMello \<martindemello@gmail.com\> wrote:
> 
> > Ruby has a built in method for converting a binary string into an  
> > integer, so your code is simply
> > 
> > def binaryStringToFloat(str)  
> > &nbsp;&nbsp;str.to\_i(2) \* 1.0/str.length  
> > end
