下面代码是关于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字符串转换成整数的代码的更多相关文章

  1. 【Java】 剑指offer(67) 把字符串转换成整数

      本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 请你写一个函数StrToInt,实现把字符串转换成整数这个功能 ...

  2. 剑指 Offer 67. 把字符串转换成整数 + 字符串

    剑指 Offer 67. 把字符串转换成整数 Offer_67 题目描述 题解分析 java代码 package com.walegarrett.offer; /** * @Author WaleGa ...

  3. 力扣 - 剑指 Offer 67. 把字符串转换成整数

    题目 剑指 Offer 67. 把字符串转换成整数 思路1 根据题意,要解决这题,首先要判断的条件有: 不包括首位空格 第一位必须为:+.-.数字三者其一,否则不合法 数字必须连续的,如果遇到非数字, ...

  4. 17.把字符串转换成整数[atoi]

    [题目] 把字符串转换成整数,需要考虑字符串有效性. [代码]  C++ Code  123456789101112131415161718192021222324252627282930313233 ...

  5. linux c/c++ IP字符串转换成可比较大小的数字

    由www.169it.com搜集整理 IP字符串转换成可比较大小的数字,具体代码如下所示: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include "stdio. ...

  6. 《剑指offer》第六十七题(把字符串转换成整数)

    // 面试题67:把字符串转换成整数 // 题目:请你写一个函数StrToInt,实现把字符串转换成整数这个功能.当然,不 // 能使用atoi或者其他类似的库函数. #include <ios ...

  7. Python使用函数实现把字符串转换成整数

    需求:假设Python没有提供内置函数int如果使用函数方式实现把一串字符串转换成整数例如把字符串‘12345‘转换成整数12345 思路 1,字符串也是序列可以使用map函数处理分割成一个列表 2, ...

  8. [Leetcode] String to integer atoi 字符串转换成整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  9. 算法练习-字符串转换成整数(实现atoi函数)

    练习问题来源 https://leetcode.com/problems/string-to-integer-atoi/ https://wizardforcel.gitbooks.io/the-ar ...

随机推荐

  1. 前端——BOM和DOM

    要想和浏览器有交互的动作,即要继续学习DOM,BOM. JavaScript分为 ECMAScript,DOM,BOM. BOM (Browser Object Model) 是指浏览器对象模型,他使 ...

  2. linux新手记录;可执行文件直接运行

    下载meshlab $sudo apt-get install meshlab 查看meshlab位置 $ whereis meshlab\meshlab: /usr/bin/meshlab /usr ...

  3. 微信小程序复选框实现 多选一功能

    功能实现界面 data: { checkboxItems: [ { name: '全天(1-8节)', value: 'allday' }, { name: '上午(1-4节)', value: 'a ...

  4. redis序列化

    private void setSerializer(StringRedisTemplate template) { Jackson2JsonRedisSerializer jackson2JsonR ...

  5. 单端测序(Single- ead)和双端测序(Pai ed-end和Mate-pai )的关系

    Roche 454,Solexa和ABI SOLID均有单端测序和双端测序两种方式.在基因组De Novo测序过程中,Roche454的单端测序读长可以达到400p,经常用于基因组骨架的组装,而Sol ...

  6. MySQL 存储过程参数

    MySQL  存储过程参数 MySQL存储过程参数简介 在现实应用中,开发的存储过程几乎都需要参数.这些参数使存储过程更加灵活和有用. 在MySQL中,参数有三种模式:IN,OUT或INOUT. IN ...

  7. MySQL 8.0.12 基于Windows 安装教程(超级详细)

    MySQL 8.0.12 基于Windows 安装教程(超级详细) (一步一步来,装不了你找我!) 本教程仅适用Windows系统,如果你原本装了没装上,一定要先删除原本的数据库,执行:mysqld ...

  8. UNDO -- Concept

     Undo data Records of the actions of transactions, primarily before they are committed. The database ...

  9. Venom- Eminem

    I got a song filled with shit for the strong willed. 我写了一首充满戾气的歌献给意志坚强的人. When the world give you a ...

  10. java mvn:安装jar包

    mvn install:install-file -Dfile=fastdfs-client-java-1.27-SNAPSHOT.jar(路径) -DgroupId=org.csource -Dar ...