原题 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 128 has the binary representation: 10000000
  • 2nd octet 32 has the binary representation: 00100000
  • 3rd octet 10 has the binary representation: 00001010
  • 4th octet 1 has 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地址转换的更多相关文章

  1. 四、IP地址转换

    IP地址与端口 TCP/IP(传输控制协议/网际协议)不是一个协议,而是一组协议的总称,包括IP.TCP.UDP.ICMP.ARP等.它规范了网络上的所有通信设备,尤其是一个主机与另一个主机之间的数据 ...

  2. 套接字编程相关函数(1:套接字地址结构、字节序转换、IP地址转换)

    1. 套接字地址结构 1.1 IPv4套接字地址结构 IPv4套接字地址结构通常也称为“网际套接字地址结构”,它以sockaddr_in命名,定义在<netinet/in.h>头文件中.下 ...

  3. IP地址转换函数

    只适用于IPV4 inet_addr函数将用点分十进制字符串表示的IPv4地址转化为用网络字节序整数表示的IPv4地址. 失败时返回INADDR_NONE. inet_aton函数完成和inet_ad ...

  4. 【网络编程一】主机字节序与网络字节序以及ip地址转换函数

    在计算机设计之初,对内存中数据的处理也有不同的方式,(低位数据存储在低位地址处或者高位数据存储在低位地址处),然而,在通信的过程中(ISO/OSI模型和TCP/IP四层模型中),数据被一步步封装(然后 ...

  5. ip地址转换

    通常,我们用点分十进制字符串表示ipv4地址(192.168.1.1),用十六进制字符串表示ipv6地址(fe80::20c:29ff:fee9:4bcc). ipv4转换函数 #include &l ...

  6. 华为上机:IP地址转换

    IP地址转换 描述: IP地址的长度为32,即有2^32-1个地址.IP地址一般采用点分十进制表示法,例如"192.168.1.1".IP地址也可以直接用一个32位的整数进行表示. ...

  7. Linux C++ - IP地址转换函数

    1. 函数用途:数字网络序本地序转换 适用类型:IP地址uint32_t类型.端口号uint16_t类型 #include<netinet/in.h> extern uint32_t nt ...

  8. [转]字符型IP地址转换成数字IP的SQL函数

    使用SQL函数可以实现许多的功能,下面为您介绍的是字符型IP地址转换成数字IP的SQL函数示例,供您参考,希望对您学习SQL函数能够有所帮助.      /**//*--调用示例       sele ...

  9. IP地址转换成Long型数字的算法

    在应用程序开发中,涉及到IP地址的存储,大部分开发人员都将其存为String(或文本类型).能否将固定格式为m.n.x.y的IP地址转换成 Long型的数字呢?答案是肯定的.在数据库层面,可以直接将结 ...

随机推荐

  1. Locust性能测试_先登录场景案例

    前言 有很多网站不登录的话,是无法访问到里面的页面的,这就需要先登录了实现场景:先登录(只登录一次),然后访问页面->我的地盘页->产品页->项目页 官方案例 下面是一个简单的loc ...

  2. 【1】【leetcode-115 动态规划】 不同的子序列

    给定一个字符串 S 和一个字符串 T,计算在 S 的子序列中 T 出现的个数. 一个字符串的一个子序列是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串.(例如," ...

  3. 移动端开发rem布局之less+媒体查询布局的原理步骤和心得

    rem即是以html文件中font-size的大小的倍数rem布局的原理:通过媒体查询设置不同屏幕宽度下的html的font-size大小,然后在css布局时用rem单位取代px,从而实现页面元素大小 ...

  4. Idea中类实现Serializable接口 引入 serialVersionUID

    idea实现Serializable接口,然后打出serialVersionUID的办法 setting>editor>Inspection>Java>Serializatio ...

  5. Mysql表字段命令alter add

    alter add命令用来增加表的字段. alter add命令格式:alter table 表名 add字段 类型 其他; 例如,在表MyClass中添加了一个字段passtest,类型为int(4 ...

  6. ServiceStack JWT 准备

    ServiceStack JWT设置 ServcieStack 自带的验证授权模块使用 sql server存储,所以我们第一步需要配置数据库的一些选项 container.Register<I ...

  7. sqoop从mysql导数据到hive报错:Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

    背景 使用sqoop从mysql导数据到hive,从本地服务器是可以访问mysql的(本地服务器是hadoop集群的一个datanode),但是sqoop导数据的时候依然连接不上mysql 报错如下: ...

  8. GoogleMap增加标记和路线轨迹的方法

    声明:本文基于JavaScript环境编写. 前言 按照目前的项目需求,我们需要在谷歌地图上标记出当前仓库的位置.司机补货的行车路径.司机当前班次需要补货的机器的位置,同时根据补货状态的不同标记成不同 ...

  9. 仿百度图片首页--HTML+CSS练手项目1【Table】

    [本文为原创,转载请注明出处] 技术[CSS+HTML]   布局[Table] 图片准备[百度图标.10张不同类型图] --------------------------------------- ...

  10. 使用Prometheus监控Linux系统各项指标

    首先在Linux系统上安装一个探测器node explorer, 下载地址https://prometheus.io/docs/guides/node-exporter/ 这个探测器会定期将linux ...