Java将ip字符串转换成整数的代码
下面代码是关于Java将ip字符串转换成整数的代码,希望对各位有较大用途。
public class IpUtil {
public static int Ip2Int(String strIp){
String[] ss = strIp.split("\.");
if(ss.length != 4){
return 0;
}
byte[] bytes = new byte[ss.length];
for(int i = 0; i < bytes.length; i++){
bytes[i] = (byte) Integer.parseInt(ss[i]);
}
return byte2Int(bytes);
}
public static String int2Ip(int intIp){
byte[] bytes = int2byte(intIp);
StringBuilder sb = new StringBuilder();
for(int i = 0; i < 4; i++){
sb.append(bytes[i] & 0xFF);
if(i < 3){
sb.append(".");
}
}
return sb.toString();
}
private static byte[] int2byte(int i) {
byte[] bytes = new byte[4];
bytes[0] = (byte) (0xff & i);
bytes[1] = (byte) ((0xff00 & i) >> 8);
bytes[2] = (byte) ((0xff0000 & i) >> 16);
bytes[3] = (byte) ((0xff000000 & i) >> 24);
return bytes;
}
private static int byte2Int(byte[] bytes) {
int n = bytes[0] & 0xFF;
n |= ((bytes[1] << 8) & 0xFF00);
n |= ((bytes[2] << 16) & 0xFF0000);
n |= ((bytes[3] << 24) & 0xFF000000);
return n;
}
public static void main(String[] args) {
String ip1 = "192.168.0.1";
int intIp = Ip2Int(ip1);
String ip2 = int2Ip(intIp);
System.out.println(ip2.equals(ip1));
}
}
Java将ip字符串转换成整数的代码的更多相关文章
- 【Java】 剑指offer(67) 把字符串转换成整数
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 请你写一个函数StrToInt,实现把字符串转换成整数这个功能 ...
- 剑指 Offer 67. 把字符串转换成整数 + 字符串
剑指 Offer 67. 把字符串转换成整数 Offer_67 题目描述 题解分析 java代码 package com.walegarrett.offer; /** * @Author WaleGa ...
- 力扣 - 剑指 Offer 67. 把字符串转换成整数
题目 剑指 Offer 67. 把字符串转换成整数 思路1 根据题意,要解决这题,首先要判断的条件有: 不包括首位空格 第一位必须为:+.-.数字三者其一,否则不合法 数字必须连续的,如果遇到非数字, ...
- 17.把字符串转换成整数[atoi]
[题目] 把字符串转换成整数,需要考虑字符串有效性. [代码] C++ Code 123456789101112131415161718192021222324252627282930313233 ...
- linux c/c++ IP字符串转换成可比较大小的数字
由www.169it.com搜集整理 IP字符串转换成可比较大小的数字,具体代码如下所示: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include "stdio. ...
- 《剑指offer》第六十七题(把字符串转换成整数)
// 面试题67:把字符串转换成整数 // 题目:请你写一个函数StrToInt,实现把字符串转换成整数这个功能.当然,不 // 能使用atoi或者其他类似的库函数. #include <ios ...
- Python使用函数实现把字符串转换成整数
需求:假设Python没有提供内置函数int如果使用函数方式实现把一串字符串转换成整数例如把字符串‘12345‘转换成整数12345 思路 1,字符串也是序列可以使用map函数处理分割成一个列表 2, ...
- [Leetcode] String to integer atoi 字符串转换成整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 算法练习-字符串转换成整数(实现atoi函数)
练习问题来源 https://leetcode.com/problems/string-to-integer-atoi/ https://wizardforcel.gitbooks.io/the-ar ...
随机推荐
- Sonar 配置及部署(windows系统)
Sonar 是一个用于代码质量管理的开放平台.通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具. 与持续集成工具(例如 Hudson/Jenkins 等)不同,Sona ...
- 生信工具汇总--OMICtools
各种生信工具: https://omictools.com/
- InstallShield Limited Edition for Visual Studio 使用
首先到https://info.flexerasoftware.com/IS-EVAL-InstallShield-Limited-Edition-Visual-Studio填写信息: 完成之后跳转到 ...
- CentOS7.4用yum安装并配置MySQL5.7
1.配置YUM源 下载MySQL源安装包 wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm 安装MySQ ...
- HDU - 3652
#include<stdio.h> #include<string.h> #include<math.h> #include<time.h> #incl ...
- Spring Boot监控与管理的实现
认识Actuator 在SpringBoot应用中引入spring-boot-starter-actuator依赖,它可以为开发团队提供系统运行的各项监控指标. 在项目中引入依赖配置如下: appli ...
- google搜索引擎爬虫爬网站原理
google搜索引擎爬虫爬网站原理 一.总结 一句话总结:从几个大站开始,然后开始爬,根据页面中的link,不断爬 从几个大站开始,然后开始爬,根据页面中的link,不断加深爬 1.搜索引擎和数据库检 ...
- SKU : Stock Keeping Unit
Stock Keeping Unit is a number assigned to a product by a retail store to identify the price, produ ...
- 用Python实现支持向量机并处理Iris数据集
SVM全称是Support Vector Machine,即支持向量机,是一种监督式学习算法.它主要应用于分类问题,通过改进代码也可以用作回归.所谓支持向量就是距离分隔面最近的向量.支持向量机就是要确 ...
- 【Code Tools】Java微基准测试工具JMH之中级篇
一.JMH中的基本概念 1)Mode Mode 表示 JMH 进行 Benchmark 时所使用的模式.通常是测量的维度不同,或是测量的方式不同.目前 JMH 共有四种模式: 1.Throughput ...