Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 1731

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T10:53:07+00:00 2024-11-25T10:53:07+00:00

Advanced use of JavaScript toString() and parseInt()

  • 62k

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 -> 2×10^1 + 5×10^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.

beginnersjavascripttutorialwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.