Base Conversion in Javascript

You might think how base conversion worked out in JavaScript. Here the solution, 

Number to Base Value 
Number.toString([radix]);
Number - Integer number that is to be converted radix - base value either 2, 8, 16 (optional parameter)

Example:
<script> 
//INTEGER TO BASE VALUE
var n = 42;
document.write(n.toString(2));
document.write(n.toString(8));
document.write(n.toString(16));

//HEX TO base
var h = 0xa;
document.write(n.toString(2));
document.write(n.toString(8));
</script>
Output:
101010
52
2a

1010
12


Any Base value to Integer value
parseInt(string, [radix]); 

string - string that is to be converted. The string can be binary value, octal value or hexadecimal value  
radix - base value either 2, 8, 16 (optional parameter)

Example:
<script> 
//Binary to integer
document.write(parseInt('111',2));

//Octal to integer
document.write(parseInt('12',8));

//Hex to integer
document.write(parseInt('0xc',16));
</script>

Output:
7
10
12


Popular posts from this blog

Simple Animation Using C

Personal Diary - A Mini Project Written in Turbo C With Graphical Interface

Random Bouncing Ball Animation