ipv4的ip字符串转化为int型
要求:
将现有一个ipv4的ip字符串(仅包含数字,点,空格), 其中数字和点之间的空格(至多一个)是合法的,比如“12 .3. 4 .62”,其他情况均为非法地址。写一个函数将ipv4地址字符串转化成int整数(只能遍历一遍字符串)。
实现思路:
1. 安全检查,null指针
2. 排除最容易的情况:长度、首/尾是否合法
3. 遍历(第2~第len-1个元素):
数字只考虑数值计算以及是否超出范围
" "前后只能为数字和"."
"."前后分别可以为数字或" ";"."不能超过3个;更新数组索引
#include<stdio.h>
#include<string.h> int is_digit(char ch)
{
return (ch>='0' && ch<='9')? 1:;
} int ipv4_to_int(char* str, int* part)
{
int len=,index=,i=;
char* ps=str; if(ps == NULL || part ==NULL)
return -; len=strlen(ps);
if(len< || len >)
return -; if(!is_digit(ps[]) || !is_digit(ps[len-]) )
return -; part[index]=part[index]*+(int)(ps[]-''); for(i=;i<len-;i++){
char ch = ps[i];
if(is_digit(ch)){
part[index]=part[index]*+(int)(ch-'');
if(part[index]>)
return -;
}
else if(ch==' ')
{
if( (is_digit(ps[i-]) && ps[i+]=='.') || \
(ps[i-]=='.' && is_digit(ps[i+])) )
continue;
else
return -;
}
else if(ch=='.')
{
if( (is_digit(ps[i-]) || ps[i-]==' ') && \
(ps[i+]==' ' || is_digit(ps[i+])) )
{
index++;
if(index>)
return -;
}
else
return -;
}
else
return -;
} part[index]=part[index]*+(int)(ps[i]-'');
if(part[index]>)
return -; return ;
} int main(int argc, char *argv[])
{
char exm[][]={
"255.255.255.255",
"0.0.0.0",
"12 .3. 4 .56",
{},
"2.3..",
"121 . 234 . 114 . 115.",
".12.4.7.7",
"123.34.55.",
"1.3.5.4.2.4",
" 123.3.4.5",
"123.45.67.67 ",
"12 3. 45.67. 67",
"12.256.0.1",
"A1.34.45.6",
"34.56.-2.5",
". . .",
}; int part[]={,,,};
int j=;
for(j=;j<;j++){
if(!ipv4_to_int(exm[j], part))
printf("ip_addr: %30s is <OK> ====> %d.%d.%d.%d\n",exm[j], part[],part[],part[],part[]);
else
printf("ip_addr: %30s is Invalid !!!\n",exm[j]); part[]=part[]=part[]=part[]=;
}
return ;
}
欢迎大家批评指正!! :-)
ipv4的ip字符串转化为int型的更多相关文章
- C++ 字符串分割,并把子字符串转换成int型整数
主要涉及到string类的两个函数find和substr: find()函数的用法: 原型:size_t find ( const string& str, size_t pos = 0 ) ...
- C语言:将16进制字符串转化为int类型值
将16进制字符串值转换为 int 整型值 此例中用 "1de" 作为测试字符串,实现代码如下: #include <stdio.h> #include <stdl ...
- 数组转集合、集合转数组、字符串数组与int型、long型数组等的转换
在项目中经常会遇到数组转集合.集合转数组.数组之间类型转换等操作 1.数组转集合 为了实现把一个数组转换成一个ArrayList,很多Java程序员会使用如下的代码: String str[] = { ...
- C语言atoi函数(将字符串转化为int)
头文件:#include <stdlib.h> atoi() 函数用来将字符串转换成整数(int),其原型为:int atoi (const char * str); [函数说明]atoi ...
- 字符串转化为int数组
String a = "1,2,3,4,5,6" String str[] = a.split(","); int array[] = new int[str. ...
- sql把varchar转化为int型
select Max(convert(int,id))from member_Info;
- c# sql在where查询语句中使用字符串变量与int型变量
使用where语句访问数据库时where语句用上文中以及定义过的变量来查询. string sql3 = string.Format("update Ships set ContainerN ...
- loadrunner 脚本开发-int型变量和字符串的相互转换
脚本开发-int型变量和字符串的相互转换 by:授客 QQ:1033553122 字符串转化为int型变量 Action2() { int j = 0; j = atoi("12345&qu ...
- 关于如何将_variant_t型转化为int型和string类型
1)将_variant_t型转化为int型 关于将_variant_t型转化为int型,网上有好多好多参考,但好多都很复杂并且还不对,其实整个转化过程就只一行代码可以搞定: _variant_t a; ...
随机推荐
- java request.getInputStream中文乱码解决方案
请求时要指定为UTF-8,中文码码完美解决 /** * * 得到请求body字符串,一般用于content-type:application/json * */ public static Strin ...
- 开机注册联通2G网络
2/3G PLMN LIST 在MM_RATCM_PLMN_LIST_CNF或NWSEL_MM_PLMN_SEARCH_CNF消息中可以查看2,3G搜到的PLMN LIST 内容如下: RAT:Rad ...
- Ionic run android失败解决方法。
今天在node.js窗口执行命令行ionic run android时就是报Error executing "adb device":error错,一开始以为是SDK不全,后来以为 ...
- MySQL数据备份概述
MySQL备份类型 热备份.温备份.冷备份 (根据服务器状态) 热备份:读.写不受影响: 温备份:仅可以执行读操作: 冷备份:离线备份:读.写操作均中止: 物理备份与逻辑备份 (从对象来分) 物理备份 ...
- jquery的select 2库,如果用js代码刷新选项?
遇到的需求,select的显示为select 2的样式. 那如果用户全选时,如何能让select 2的样式更新呢? 1,引入select 2 $(".select2_single" ...
- mysql binlog空间维护
默认情况下,mysql主从同步的binlog日志,会一直保存. 对于如果已同步好的数据,这显然比较浪费资源. 且如果生产环境磁盘太小,随时还会爆掉,所以很有必要作好binlog的空间维护. 以下操作, ...
- ValueError: Dependency on app with no migrations: customuser
You haven't run manage.py makemigrations customuser to create the migrations for your CustomUser app ...
- 【redis】设置密码
1.第一种方式 (当前这种linux配置redis密码的方法是一种临时的,如果redis重启之后密码就会失效,) (1)首先进入redis,如果没有开启redis则需要先开启:[root@iZ94jz ...
- Python面向对象 | isinstance和issubclass
isinstance(a,b):判断a是否是b类(或者b类的基类)实例化的对象 class A: pass class B(A): pass obj = B() print(isinstance(ob ...
- 异常CLRDBG_NOTIFICATION_EXCEPTION_CODE( 0x04242420)的抛出过程
新建一个c#控制工程,就用自动生成的代码,不用补任何代码,如下: using System; using System.Collections.Generic; using System.Linq; ...