原题 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. mysql修改后启动my.cnf报错Starting MySQL... ERROR! The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid).

    mysql中文乱码解决 mysql修改my.cnf后启动报错Starting MySQL... ERROR! The server quit without updating PID file (/v ...

  2. flickity:支持触摸滑动,响应迅速的幻灯片轮播插件

    简介:flickity 是一个支持触摸,响应迅速的幻灯片轮播插件.支持环绕滑动.自由滑动.组滑动.自动播放.延迟加载.视差滑动.图片滑动.兼容IE10+, Android 4+, Safari for ...

  3. dexlib2的源码框架

    这个是dexlib2的目录,明显看出来比baksmali和smali代码量要多很多,这里先将核心目录给大家做一下介绍 analysis 这个暂时不知道具体作用 base     这个文件夹下面全部都是 ...

  4. ETC1/DXT1 compressed textures are not supported when publishing to iPhone

    Build application in Unity 2017.20f3 用Unity2017/2018编译iPhone版本出现以下错误: ETC1(or DXT1) compressed textu ...

  5. json模块中dumps()与dump()区别

    dumps是将dict转化成json字符串格式,loads是将json字符串转化成dict格式. dump和load也是类似的功能,只是与文件操作结合起来了. dump(写入内容,文件对象) 和loa ...

  6. Ubuntu16.04 网络配置

    Ubuntu 网络配置 安装Ubuntu操作系统之后,为了通过Xshell连接主机,或者连接其他主机.需要进行如下网络配置和ssh服务配置. 1 网络配置 1.1 修改网络配置信息 sudo vi / ...

  7. centos8 网络配置

    目录 centos8已经发布了,下载了一个体验一下,新安装好的centos8默认网卡是没有启动的,安装好后需要先配置网络.在/etc/sysconfig/network-scripts目录下存放着网卡 ...

  8. secureCRT 中各种传输协议分析 启动daemon运行守护进程(转)

    转载链接:http://blog.sina.com.cn/s/blog_61798d5d01018yk4.html [Telnet]著名的终端访问协议,传统的网络服务程序,如FTP.POP和Telne ...

  9. ELK-Elasticsearch 基础使用

    一.基本概念 1 Node 与 Cluster Elastic 本质上是一个分布式数据库,允许多台服务器协同工作,每台服务器可以运行多个 Elastic 实例.单个 Elastic 实例称为一个节点( ...

  10. 分布式存储-ceph

    1. ceph 简介 Ceph是一种为优秀的性能.可靠性和可扩展性而设计的统一的.分布式文件系统().ceph 的统一体现在可以提供文件系统.块存储和对象存储,分布式体现在可以动态扩展.在国内一些公司 ...