Convert arbitary string to number between 0 and 1 in Javascript
<p>I'm working with Youtube videos which have ids like 8DnKOc6FISU, rNsrl86inpo, 5qcmCUsw4EQ (i.e. 11 characters in the set A-Za-z0-9_-)</p>

<p>The goal is to convert each id to a colour (represented by the range 0-1), so they can be reliably charted.</p>

<p>According to <a href="http://stackoverflow.com/questions/6180138/whats-the-youtube-video-id-maximum-length">this question</a> these are 64 bit numbers.  Given that:</p>

<ul>
<li>I want to make full use of the colour space for any given set of videos</li>
<li>Colour perception isn't that accurate anyway</li>
</ul>

<p>...it seems sensible to base this off the last 2-3 characters of the id.</p>

<p>My lead approach is a function I borrowed from <a href="http://blog.sarathonline.com/2009/02/javascript-convert-strings-to-binary.html" rel="nofollow">here</a>, which converts each character into a binary number like so:</p>

<pre><code>    function toBin(str){
     var st,i,j,d;
     var arr = [];
     var len = str.length;
     for (i = 1; i&lt;=len; i++){
      d = str.charCodeAt(len-i);
      for (j = 0; j &lt; 8; j++) {
       st = d%2 == '0' ? "class='zero'" : "" 
       arr.push(d%2);
       d = Math.floor(d/2);
      }
     }
    }
</code></pre>

<p>But this leaves the question of how to convert this back to a float.</p>

<p>Any ideas for an elegant solution?</p>

<p>Many thanks for your help!</p>
