Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The use of IP Converter

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "the use of IP converter". In daily operation, I believe many people have doubts about the use of IP converter. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "the use of IP converter". Next, please follow the editor to study!

Today's topic is: IP Converter. It's really just a way to implement an IP transformation.

Topic (difficulty 6kyu:IPv4 to int32):

A brief translation:

There is an IPv4 address: 128.32.10.1. This address actually consists of four 8-bit bytes:

The first 128, its binary is: 10000000

The second 32, its binary is: 00100000

The third 10, its binary is: 00001010

The fourth 1, its binary is: 00000001

So, 128.32.10.1 is equivalent to 10000000.00100000.00001010.00000001

In that case, the IP address is 32-bit, so we get a number from the 32-bit byte: 2149583361.

Request:

Write a function: ip_to_int32 (ip) allows the address of this IPv4 to return that number:

Iptoint32 ("128.32.10.1") = 2149583361

Ideas for solving the problem:

It can be seen that most of the topics of codewars come from some basic knowledge in practice. The implicit knowledge here is:

Why convert IP to a number?

According to the TCP/IP protocol, the IP address is composed of 32-bit binary numbers. to put it simply, it is easy to query or do some login restrictions set according to IP, such as some IP databases, which store numbers converted into 32-bit binaries.

Only on the premise of knowing this basic knowledge can the topic become meaningful.

So my initial idea of solving the problem is as follows:

1. You need to convert every ip number in the string "128.32.10.1" to binary.

two。 Splice 4 segments of binaries together to form a 32-bit binary string.

3. Convert a 32-bit binary string into a decimal.

So I got the following method:

Def ip_to_int32 (ip) ip.gsub (/ (\ d +) /) {| I | I = i.to_i.to_s (2); "0" * (8-i.size)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report