From: Dr J R Stockton on
Some may not have noted that Number.toString(16) handles non-integers.
Math.PI.toString(16) -> 3.243f6a8885a3
1e44.toString(16) -> 47bf19673df53000000000000000000000000
Math.random().toString(2) => 0 . /[01]{1,53}/

ECMA 262 (5) 15.7.4.2 Number.prototype.toString ( [ radix ] )
makes no mention of non-integers.

A Hex integer string H can be turned into a Number by
+ ( "0x" + H )
but that does not work for a Hex fraction string. Is there in fact an
easy built-in way of converting non-integer Hex strings to Number?

Otherwise, I suggest that ECMA 6 should specify a Method whereby for ANY
Number X the result of X.toString(B) can be converted to the original X
(disregarding the sign of zero).

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Grandson-Of-RFC1036 is released. RFC 5536 Netnews Article Format is a
subset of Internet Message Format which is described in RFC 5532. The
RFCs are read together to determine standard Netnews article format.
From: Ry Nohryb on
On May 26, 10:44 pm, Dr J R Stockton <reply1...(a)merlyn.demon.co.uk>
wrote:
> Some may not have noted that Number.toString(16) handles non-integers.
>         Math.PI.toString(16) -> 3.243f6a8885a3
>         1e44.toString(16) -> 47bf19673df53000000000000000000000000
>         Math.random().toString(2) => 0 . /[01]{1,53}/

My dear friend Garrett and I know that at least since:
http://groups.google.com/group/comp.lang.javascript/msg/936a683de6159fd0

> ECMA 262 (5) 15.7.4.2 Number.prototype.toString ( [ radix ] )
> makes no mention of non-integers.
>
> A Hex integer string H can be turned into a Number by
>         + ( "0x" + H )
> but that does not work for a Hex fraction string.  Is there in fact an
> easy built-in way of converting non-integer Hex strings to Number?

a= 113356057.78731406
--> 113356057.78731406

b= a.toString(16).split('.');
--> [ "6c1ad19", "c98d6a" ]

c= +( '0x'+ b.join('') ) / Math.pow(16, b[1].length)
--> 113356057.78731406

> Otherwise, I suggest that ECMA 6 should specify a Method whereby for ANY
> Number X the result of X.toString(B) can be converted to the original X
> (disregarding the sign of zero).
--
Jorge.
From: Evertjan. on
Ry Nohryb wrote on 27 mei 2010 in comp.lang.javascript:

> a= 113356057.78731406
> --> 113356057.78731406
>
> b= a.toString(16).split('.');
> --> [ "6c1ad19", "c98d6a" ]
>
> c= +( '0x'+ b.join('') ) / Math.pow(16, b[1].length)
> --> 113356057.78731406

or just add the fractional part to the integer part.

===========================================

Try this for any radix [0 .. 36]:

<script type='text/javascript'>

document.write(radixTo10('6c1ad19.c98d6a',16) + '<br>')
// --> 113356057.78731406
document.write(radixTo10('778',8) + '<br>')
// --> 511
document.write(radixTo10('z',36) + '<br>')
// --> 35
document.write(radixTo10('10',36) + '<br>')
// --> 36


function radixTo10int(v,radix) {
var n = 0;
for (i=0;i<v.length;i++) {
temp = v[i];
temp = (temp>'9')
? 10+temp.charCodeAt(0)-'a'.charCodeAt(0)
: +temp;
//if (temp>radix-1) return 'error';
n *= radix;
n += temp;
};
return n;
};

function radixTo10(v,radix) {
v = v.toLowerCase().split('.');
if (v.length==1)
return radixTo10int(v[0],radix);
return radixTo10int(v[0],radix) +
radixTo10int(v[1],radix) / Math.pow(radix,v[1].length);
};

</script>


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
From: Evertjan. on
Evertjan. wrote on 27 mei 2010 in comp.lang.javascript:

> Ry Nohryb wrote on 27 mei 2010 in comp.lang.javascript:
>
>> a= 113356057.78731406
>> --> 113356057.78731406
>>
>> b= a.toString(16).split('.');
>> --> [ "6c1ad19", "c98d6a" ]
>>
>> c= +( '0x'+ b.join('') ) / Math.pow(16, b[1].length)
>> --> 113356057.78731406
>
> or just add the fractional part to the integer part.
>
> ===========================================
>
> Try this for any radix [0 .. 36]:
>
> <script type='text/javascript'>
>
> document.write(radixTo10('6c1ad19.c98d6a',16) + '<br>')
> // --> 113356057.78731406
> document.write(radixTo10('778',8) + '<br>')
> // --> 511

sorry:

document.write(radixTo10('777',8) + '<br>')
// --> 511


> document.write(radixTo10('z',36) + '<br>')
> // --> 35
> document.write(radixTo10('10',36) + '<br>')
> // --> 36
>
>
> function radixTo10int(v,radix) {
> var n = 0;
> for (i=0;i<v.length;i++) {
> temp = v[i];
> temp = (temp>'9')
> ? 10+temp.charCodeAt(0)-'a'.charCodeAt(0)
> : +temp;
> //if (temp>radix-1) return 'error';
> n *= radix;
> n += temp;
> };
> return n;
>};
>
> function radixTo10(v,radix) {
> v = v.toLowerCase().split('.');
> if (v.length==1)
> return radixTo10int(v[0],radix);
> return radixTo10int(v[0],radix) +
> radixTo10int(v[1],radix) / Math.pow(radix,v[1].length);
>};
>
> </script>
>
>



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
From: Ry Nohryb on
On May 27, 11:11 am, "Evertjan." <exjxw.hannivo...(a)interxnl.net>
wrote:
> Ry Nohryb wrote on 27 mei 2010 in comp.lang.javascript:
>
> > a= 113356057.78731406
> > --> 113356057.78731406
>
> > b= a.toString(16).split('.');
> > --> [ "6c1ad19", "c98d6a" ]
>
> > c= +( '0x'+ b.join('') ) / Math.pow(16, b[1].length)
> > --> 113356057.78731406
>
> or just add the fractional part to the integer part.
>
> ===========================================
>
> Try this for any radix [0 .. 36]:
>
> <script type='text/javascript'>
>
> document.write(radixTo10('6c1ad19.c98d6a',16) + '<br>')
> // --> 113356057.78731406
> document.write(radixTo10('778',8) + '<br>')
> // --> 511
> document.write(radixTo10('z',36) + '<br>')
> // --> 35
> document.write(radixTo10('10',36) + '<br>')
> // --> 36
>
> function radixTo10int(v,radix) {
>   var n = 0;
>   for (i=0;i<v.length;i++) {
>     temp = v[i];
>     temp = (temp>'9')
>       ? 10+temp.charCodeAt(0)-'a'.charCodeAt(0)
>       : +temp;
>     //if (temp>radix-1) return 'error';
>     n *= radix;
>     n += temp;
>   };
>   return n;
>
> };
>
> function radixTo10(v,radix) {
>   v = v.toLowerCase().split('.');
>   if (v.length==1)
>     return radixTo10int(v[0],radix);
>   return radixTo10int(v[0],radix) +
>   radixTo10int(v[1],radix) / Math.pow(radix,v[1].length);
>
> };
>
> </script>

Cute. How about this one :-) ?

String.prototype.toFP= function (base, digits, r, w, n) {
digits= "0123456789abcdefghijklmnopqrstuvwxyz", r= 0;
w= (n= this.toLowerCase().split('.'))[0].length;
n.join('').split('').forEach(function (s) {
r+= digits.indexOf(s) * Math.pow(base, --w) });
return r;
};



a= Math.PI
--> 3.141592653589793
b= a.toString(16)
--> "3.243f6a8885a3"
a.toFP(16)
--> 3.141592653589793
b= a.toString(36)
--> "3.53i5ab8p5fc5vayqter60f6r"
b.toFP(36)
--> 3.141592653589793
b= a.toString(2)
--> "11.001001000011111101101010100010001000010110100011"
b.toFP(2)
--> 3.141592653589793
--
Jorge.