javascript
JavaScript | 将十进制转换为十六进制,反之亦然
Sometimes we need to convert an integer value which is in decimal format to the hexadecimal string in JavaScript or need a decimal value from a given hexadecimal string.
有時,我們需要將十進制格式的整數值轉換為JavaScript中的十六進制字符串,或者需要給定十六進制字符串中的十進制值 。
Read more: How to assign decimal, octal and hexadecimal values to the variables in JavaScript?
: 如何為JavaScript中的變量分配十進制,八進制和十六進制值?
In such cases, we can convert a decimal value to the hexadecimal string by using the number.toString(16) and hexadecimal string to the decimal by using hex_string.parseInt(16).
在這種情況下,我們可以通過使用number.toString(16)和十六進制字符串到十進制通過使用hex_string.parseInt(16) 轉換成十進制值以十六進制字符串 。
In both cases, 16 is the base to the number system that says that target or source value format is hexadecimal.
在這兩種情況下,16是基座到數字系統 ,指出目標或源值格式是十六進制。
Example:
例:
<html> <head> <title>JavaScipt Example</title> </head><body><script> var a = 10;var b = 12345;document.write("a = " + a.toString(16) + "<br>");document.write("b = " + b.toString(16) + "<br>");//conver hexadecimal string to the int againvar hex = "0A"; //hex of 10var dec = parseInt(hex,16);document.write("dec = " + dec + "<br>");</script> </body> </html>Output
輸出量
a = a b = 3039 dec = 10翻譯自: https://www.includehelp.com/code-snippets/convert-decimal-to-hexadecimal-and-vice-versa-in-javascript.aspx
總結
以上是生活随笔為你收集整理的JavaScript | 将十进制转换为十六进制,反之亦然的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RabbitMQ中7种消息队列和保姆级代
- 下一篇: 再见收费的Navicat!操作所有数据库