DEV Community

Vivek Kumar
Vivek Kumar

Posted on

Advanced use of JavaScript toString() and parseInt()

You might have been using toString() method and parseInt() in javaScript in day to day coding life.

One basic use case is like converting a number to string with the help of toString() method

const n = 10;
console.log(n.toString())
//Output
//"10"
Enter fullscreen mode Exit fullscreen mode

and converting a "10" to number using parseInt().

const str = "10";
console.log(parseInt(str));
//Output
//10
Enter fullscreen mode Exit fullscreen mode

If you know this means then you understand the basics of toString() and parseInt().

Lets understand it further:

toString()

When you use .toString()
There is an optional parameter called base.

You might have heard about number systems in Mathematics.

The number system which we use in day to day life is decimal.

A Decimal System uses 10 as base. We can represent it as
25 -> 2x10^1 + 5x10^0

I am assuming you understand above representation.

There are few famous representations such as Hexadecimal, Octal and Binary.
Hexadecimal - base 16
Octal - base 8
Binary - base 2

I understand I moved away from the main agenda of this discussion. But the reason behind I was explaining about number system is we can use these bases to convert a number from one type to another.

Lets say we need to convert "25" to binary

 const a = 25;
 a.toString(2) 
 // Output
 // 11001
Enter fullscreen mode Exit fullscreen mode

Or we want to convert "111" to hexadecimal

 const a = 111;
 a.toString(16) 
 // Output
 // 6f
Enter fullscreen mode Exit fullscreen mode

Or we want to convert "43" to octal

 const a = 43;
 a.toString(8) 
 // Output
 // 53
Enter fullscreen mode Exit fullscreen mode

There can be many use cases where you want to do these conversions one example is when you are dealing with buffer and you want to decode.

Lets take another example,
We want to convert text "Hello" to binary

How we will do this ?

 Array.from("Hello").map(char => char.charCodeAt(0).toString(2));
// Output
[ "1001000", "1100101", "1101100", "1101100", "1101111" ]

Enter fullscreen mode Exit fullscreen mode

If you add prefix to each no with 0b, you can use it with TypedArray View ( Uint8Array, Uint16Array or Uint32Array, etc)

const unsignedArray = new Uint8Array([ "0b1001000", "0b1100101", "0b1101100", "0b1101100", "0b1101111" ])
Enter fullscreen mode Exit fullscreen mode

You can use it as stream or convert it to blob.

Again I don't want to go deep in Unit8Array. But just for understanding, we can use '0x' as hexadecimal, '0o' for octal.

If you convert this to hexadecimal, then following will be the output

Array.from("Hello").map(char => '0x'+char.charCodeAt(0).toString(16)); 
// Output
// [ "0x48", "0x65", "0x6c", "0x6c", "0x6f" ]
Enter fullscreen mode Exit fullscreen mode

And I am sure, you must have seen these type of data specially while dealing with buffers.

parseInt()

So, parseInt() method accepts first parameter as data we want to covert to integer and second optional parameter as base.

Whatever data you pass inside parseInt() it will always try to convert it to a number or you can say to a decimal number to be exact that means its base will be 10.

lets say we have a binary number 1001000 and we want to convert it to decimal. Then we can use parseInt()

const num = parseInt(1001000, 2) // base 2

console.log(num)
//OUTPUT
// 72
Enter fullscreen mode Exit fullscreen mode

and 72 is ASCII value of 'H'

String.fromCharCode(72)
// Output
// 'H'
Enter fullscreen mode Exit fullscreen mode

You can experiment further and use it with other number system like hexadecimal.

If you want to learn about ArrayBuffer, Uint8Array, Uint16Array, etc just comment below.

Top comments (0)