Home > Web Front-end > JS Tutorial > Convert unsigned 32-bit decimal to corresponding ipv4 address in JavaScript

Convert unsigned 32-bit decimal to corresponding ipv4 address in JavaScript

WBOY
Release: 2023-09-07 08:01:11
forward
1369 people have browsed it

在 JavaScript 中将无符号 32 位十进制转换为相应的 ipv4 地址

Question

Consider the following ipv4 address -

128.32.10.1
Copy after login
Copy after login

If we convert it to binary, the equivalent is -

10000000.00100000.00001010.00000001
Copy after login

Additionally, if we convert this binary to unsigned 32-bit decimal, the decimal will be -

2149583361
Copy after login

Therefore, we can say that the ipv4 equivalent of 2149583361 is 128.32.10.1

We need to write a JavaScript function that accepts a 32-bit unsigned integer and returns its equivalent ipv4 address. < /p>

Example

The following is the code-

Real-time demonstration

const num = 2149583361;
const int32ToIp = (num) => {
   return (num >>> 24 & 0xFF) + &#39;.&#39; +
   (num >>> 16 & 0xFF) + &#39;.&#39; +
   (num >>> 8 & 0xFF) + &#39;.&#39; +
   (num & 0xFF);
};
console.log(int32ToIp(num));
Copy after login

Output

The following is the console output-

128.32.10.1
Copy after login
Copy after login

The above is the detailed content of Convert unsigned 32-bit decimal to corresponding ipv4 address in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template