[codewars] - int32 to IPv4 二进制十进制 ip地址转换
原题 https://www.codewars.com/kata/int32-to-ipv4/train/java
Take the following IPv4 address: 128.32.10.1
This address has 4 octets where each octet is a single byte (or 8 bits).
- 1st octet
128has the binary representation:10000000 - 2nd octet
32has the binary representation:00100000 - 3rd octet
10has the binary representation:00001010 - 4th octet
1has the binary representation:00000001
So 128.32.10.1 == 10000000.00100000.00001010.00000001
Because the above IP address has 32 bits, we can represent it as the unsigned 32 bit number: 2149583361
Complete the function that takes an unsigned 32 bit number and returns a string representation of its IPv4 address.
Examples
2149583361 ==> "128.32.10.1"
32 ==> "0.0.0.32"
0 ==> "0.0.0.0"
solution :
public class Kata {
public static String longToIP(long ip) {
//1. translate the ip to binary representation
String str = "";
if (ip == 0) {
str = ip + "";
} else {
while (ip != 0) {
str = ip % 2 + str;
ip = ip / 2;
}
}
//2. if the binary string short than 32 bit, then add "0" in front
while (str.length() != 32) {
str = "0" + str;
}
String result = "";
//3. truncate the str to four items
for (int i = 0; i < 4; i++) {
String partStr = str.substring(i * 8, 8 * (i + 1));
//4. translate every item to decimal number
int bi = Integer.parseInt(partStr, 2);
if (i == 3) {
result += bi + "";
} else {
result += bi + ".";
}
}
return result;
}
}
CW上的大神解:
1.
public class Kata {
public static String longToIP(long ip) {
return String.format("%d.%d.%d.%d", ip>>>24, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
}
}
2.
public class Kata {
public static String longToIP(long ip) {
String[] e = new String[4];
int i = 4;
while (i-- != 0) {
e[i] = "" + (ip % 256);
ip /= 256;
}
return String.join(".",e);
}
}
3.
public class Kata {
public static String longToIP(long ip) {
return IntStream.rangeClosed(1, 4)
.mapToObj(i -> String.valueOf(ip >> (32 - 8 * i) & 255))
.collect(Collectors.joining("."));
}
}
[codewars] - int32 to IPv4 二进制十进制 ip地址转换的更多相关文章
- 四、IP地址转换
IP地址与端口 TCP/IP(传输控制协议/网际协议)不是一个协议,而是一组协议的总称,包括IP.TCP.UDP.ICMP.ARP等.它规范了网络上的所有通信设备,尤其是一个主机与另一个主机之间的数据 ...
- 套接字编程相关函数(1:套接字地址结构、字节序转换、IP地址转换)
1. 套接字地址结构 1.1 IPv4套接字地址结构 IPv4套接字地址结构通常也称为“网际套接字地址结构”,它以sockaddr_in命名,定义在<netinet/in.h>头文件中.下 ...
- IP地址转换函数
只适用于IPV4 inet_addr函数将用点分十进制字符串表示的IPv4地址转化为用网络字节序整数表示的IPv4地址. 失败时返回INADDR_NONE. inet_aton函数完成和inet_ad ...
- 【网络编程一】主机字节序与网络字节序以及ip地址转换函数
在计算机设计之初,对内存中数据的处理也有不同的方式,(低位数据存储在低位地址处或者高位数据存储在低位地址处),然而,在通信的过程中(ISO/OSI模型和TCP/IP四层模型中),数据被一步步封装(然后 ...
- ip地址转换
通常,我们用点分十进制字符串表示ipv4地址(192.168.1.1),用十六进制字符串表示ipv6地址(fe80::20c:29ff:fee9:4bcc). ipv4转换函数 #include &l ...
- 华为上机:IP地址转换
IP地址转换 描述: IP地址的长度为32,即有2^32-1个地址.IP地址一般采用点分十进制表示法,例如"192.168.1.1".IP地址也可以直接用一个32位的整数进行表示. ...
- Linux C++ - IP地址转换函数
1. 函数用途:数字网络序本地序转换 适用类型:IP地址uint32_t类型.端口号uint16_t类型 #include<netinet/in.h> extern uint32_t nt ...
- [转]字符型IP地址转换成数字IP的SQL函数
使用SQL函数可以实现许多的功能,下面为您介绍的是字符型IP地址转换成数字IP的SQL函数示例,供您参考,希望对您学习SQL函数能够有所帮助. /**//*--调用示例 sele ...
- IP地址转换成Long型数字的算法
在应用程序开发中,涉及到IP地址的存储,大部分开发人员都将其存为String(或文本类型).能否将固定格式为m.n.x.y的IP地址转换成 Long型的数字呢?答案是肯定的.在数据库层面,可以直接将结 ...
随机推荐
- 一文读懂 IPv4 到 IPv6 的过渡技术
在介绍 IPv4 到 IPv6 过渡技术之前,我们先来简单了解一下 IPv4 和 IPv6.什么是 IPv4?IPv4 全称为 Internet Protocol version 4,它为互联网上的每 ...
- Git diff (---和+++具体解释)(转)
转自:https://blog.csdn.net/lovezbs/article/details/46492933
- vue-cli输入命令vue ui没效果
最近用vue-cli脚手架很顺口,特别是UI控制台,在这里,创建项目和搭建本地环境,连接服务端变得很容易,页面ui也是一流 要怎么启动呢?在终端输入命令行vue ui,启动UI控制台,然后往浏览器输入 ...
- Bash速查表
例 #!/usr/bin/env bash NAME="John" echo "Hello $NAME!" 变量 NAME="John" e ...
- .NET-异步操作
感觉可以用于log日志的东西,这个东西他还是会走的但是不会影响你下一步的操作,你下一步还是正常怎么操作就怎么操作! 这样可以给用户免掉一些没必要的等待. static void Main(string ...
- Tensorflow在python3.7版本的运行
安装tensorflow pip install tensorflow==1.13.1 -i https://pypi.tuna.tsinghua.edu.cn/simple 可以在命令行 或者在py ...
- win 修改notebook路径
开始发现 notebook 默认的路径是 C:\Users\Administrator 需要修改 将目标中的%USERPROFILE% 直接删掉了
- Java知识回顾 (15) 文档注释
说明注释允许你在程序中嵌入关于程序的信息. 你可以使用 javadoc 工具软件来生成信息,并输出到HTML文件中,使你更加方便的记录你的程序信息. javadoc 标签 标签 描述 示例 @auth ...
- 缓冲区溢出漏洞 ms04011
DSScan使用 扫描目标主机是否存在ms04011漏洞 getos使用 获取操作系统类型 > getos.exe 192.168.1.101 ------------------------- ...
- postman 在pre-request中发送application/x-www-form-urlencoded 格式表单
postman中在pre-request 发送请求 知识点: json数据解析和遍历 application/x-www-form-urlencoded表单 Array基本使用 js函数 http请求 ...