Number conversions using parseInt() and toString() in JavaScript

Anyone who codes in JavaScript regularly has made use of parseInt() to convert an integer that's expressed as a string into an actual number type that one can use in calculations.

The MDN defines the use of parseInt() as follows:

复制代码
parseInt(string)
parseInt(string, radix)

string is the value to parse. In the case that it's not a string type, it is internally converted to a string before the parsing occurs.

radix is a value between 2 and 36 which is the base of the string parameter value. It does NOT default to base 10 if this parameter is omitted. For example, a string with the prefix "0x" will default to base 16, hexadecimal. To learn in greater detail how various inputs are treated, please refer to the MDN article on parseInt() for more details.

parseInt()

parseInt() can be used to convert numbers to and from various bases if one understands how the radix parameter functions.

For example:

复制代码
parseInt("23", 4)
parseInt("0x23")
parseInt("23", 2)

will output:

复制代码
11
35
NaN

In each case we are answering the question: "What is the base 10 value of 23 in base 4? in base 16? in base 2?"

  1. "23" in base 4 is 2 * 4 + 3, or 11 in base 10.
  2. "0x23" implies base 16. 2 * 16 + 3, or 35 in base 10
  3. "23" in base 2 is NaN, since base 2 digits can only include 0 and 1.

Imagine how useful this could be if someone needs you to quickly interpret binary "1110010"!

复制代码
parseInt("1110010", 2)

will give us the correct base 10 number:

复制代码
114

toString(radix)

Using toString(radix) with a number object overrides the the Object object method toString(). When used with number objects, toString(radix) returns a string representation of the number object in the specified base.

toString(radix) answers the question: "How do I write the value 23 in base 4? in base 16? in base 2?"

复制代码
let num = 23

num.toString(4)
num.toString(16)
num.toString(2)

And the output:

复制代码
113
17
10111

In effect, parseInt() and toString() act as inverses of each other:

复制代码
let num = 23

parseInt(num.toString(4), 4)
parseInt(num.toString(16), 16)
parseInt(num.toString(2), 2)

And the result:

复制代码
23
23
23

toString(2) is particularly useful if you need to generate the binary representation of a number for any reason. Or how about converting a hexadecimal number (base 16) into its binary equivalent?

复制代码
parseInt("1F4", 16).toString(2)

"111110100"

(in case you were wondering, this is 500 in base 10!)

I hope this brief exploration of these two related methods leaves you with a greater understanding of how they go beyond converting integers to strings and vice versa. They can be used to translate numbers to and from other bases, as well!

At last ,If you want to protects your javascript code from being illegally analyzed, hacked, and stolen.You can obfuscate js with js-obfuscator or jshaman etc.

Happy Coding!

相关推荐
Evand J26 分钟前
【matlab例程】无迹粒子滤波(UPF)的例程,用于三维环境下多雷达目标跟踪,非线性系统
开发语言·matlab·目标跟踪
2501_924878731 小时前
无人机光伏巡检缺陷检出率↑32%:陌讯多模态融合算法实战解析
开发语言·人工智能·算法·视觉检测·无人机
计算机毕设定制辅导-无忧学长1 小时前
InfluxDB 与 Python 框架结合:Django 应用案例(三)
开发语言·python·django
惜.己1 小时前
python中appium
开发语言·python·appium
小沈熬夜秃头中୧⍤⃝1 小时前
Python 入门指南:从零基础到环境搭建
开发语言·python
睿思达DBA_WGX1 小时前
Python 程序设计讲义(54):Python 的函数——函数概述
开发语言·python
Algebraaaaa1 小时前
C++ 中 NULL 与 nullptr 有什么区别?
开发语言·c++
吃饭睡觉打豆豆嘛1 小时前
彻底搞懂前端路由:从 Hash 到 History 的演进与实践
前端·javascript
tju新生代魔迷1 小时前
C语言:位段作业
c语言·开发语言
-$_$-2 小时前
【笔试真题】2024秋招京东后端开发岗位-第一批笔试
java·开发语言