C语言实现字符串IP与整数型IP的相互转换
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <malloc.h>
- int main(int argc,char *argv[])
- {
- const char *ip ="192.168.34.232";
- char *ipstr=NULL;
- char str_ip_index[4]={'\0'};
- unsigned int ip_int,ip_add=0,ip_int_index[4],ip_temp_numbr=24;
- int j =0,a=3;
- for(unsigned int i =0;i<=strlen(ip);i++)//要用到'\0'
- {
- if (ip[i]=='\0'||ip[i]=='.')
- {
- ip_int =atoi(str_ip_index);
- if (ip_int<0||ip_int>255)
- {
- printf("IP地址有误\n");
- system("pause");
- return 0;
- }
- ip_add+=(ip_int*((unsigned int)pow(256.0,a)));
- a--;
- memset(str_ip_index,0,sizeof(str_ip_index));
- // for (int x=0;x<4;x++)
- // {
- // str_ip_index[x]='\0';
- // }
- j=0;
- continue;
- }
- str_ip_index[j]=ip[i];
- j++;
- }
- printf("%u\n",ip_add);
- //转换成x.x.x.x
- for(j=0;j<4;j++)
- {
- ip_int_index[j]=(ip_add>>ip_temp_numbr)&0xFF;
- ip_temp_numbr-=8;
- }
- if ((ipstr=(char *)malloc(17*sizeof(char)))==NULL)
- {
- return 0;
- }
- sprintf(ipstr,"%d.%d.%d.%d",ip_int_index[0],ip_int_index[1],ip_int_index[2],ip_int_index[3]);
- printf("%s\n",ipstr);
- free(ipstr);
- ipstr=NULL;
- system("pause");
- return 0;
- }
转换效果:
C语言实现字符串IP与整数型IP的相互转换的更多相关文章
- python实现判断一个字符串是否是合法IP地址
#!usr/bin/env python #encoding:utf-8 ''''' __Author__:沂水寒城 功能:判断一个字符串是否是合法IP地址 ''' import re def jud ...
- 判断一个字符串是否是合法IP地址
# -*- coding: utf-8 -*- """ @File:test06_判断ip地址是否合法.py @E-mail:364942727@qq.com @Time ...
- Java使用纯真IP库获取IP对应省份和城市
原文:http://blog.csdn.net/chwshuang/article/details/78027873?locationNum=10&fps=1 Java使用纯真IP库获取IP对 ...
- go语言之字符串、指针、数组、切片、结构struct、面向对象
一: 字符串 概述: Go 语言将字符串作为 种原生的基本数据类型,字 符串的初始化可以使用字符串字面量. (1)字符串是常量,可以通过类 数组 索引访问其字节单元,但是不能修改某个字节的值 (2)宇 ...
- [LeetCode] Validate IP Address 验证IP地址
In this problem, your job to write a function to check whether a input string is a valid IPv4 addres ...
- [LeetCode] Restore IP Addresses 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- c#中获取服务器IP,客户端IP以及Request.ServerVariables详细说明
客户端ip: Request.ServerVariables.Get("Remote_Addr").ToString(); 客户端主机名: Request.ServerVaria ...
- C#获取本机IP以及无线网ip
1 private void GetIP() 2 { 3 string hostName = Dns.GetHostName();//本机名 4 //System.Net.IPAddress ...
- node.js使用免费的阿里云ip查询获取ip所在地
在项目过程中,我们常常需要获取IP的所在地.而这一功能一般都是通过一些数据网站的对外接口来实现,这些接口一般情况下都是付费使用的.在这篇文章中我将记录,基于node.js的阿里云免费IP地址查询接口的 ...
随机推荐
- ssh : how to add "hostkey" to “know_hosts”
有时后端daemon或者脚本在执行ssh连接时,会遇到以下提示: The authenticity of host 'git.sws.com (10.42.1.88)' can't be establ ...
- 《FPGA全程进阶---实战演练》第八章之程序架构格式说明
首先在书写程序时必须有的部分,就是模块module部分,整体的架构如图8.1所示. 图8.1 程序整体架构 首先要声明模块名,在module后面加上模块名,这里最好以所建立模块要实现的功能去命名此模块 ...
- Build 2016上一些令人兴奋的东西
本来应该要更新Windows IOT开发入门的,但是由于Build 2016刚刚过去,不得不将一些令人兴奋的东西给大家进行分享. 首先对于错过这次直播的,可以在以下链接再次看到观看: https:// ...
- C语言实现Linux下删除非空目录
#include <sys/stat.h> #include <dirent.h> #include <fcntl.h> /** * 递归删除目录(删除该目录以及该 ...
- java中String new和直接赋值的区别
Java中String new和直接赋值的区别 对于字符串:其对象的引用都是存储在栈中的,如果是编译期已经创建好(直接用双引号定义的)的就存储在常量池中,如果是运行期(new出来的)才 ...
- (笔记)Mysql命令select from:查询表中的数据(记录)
select from命令用来查询表中的数据. 1) 查询所有行命令格式: select <字段1, 字段2, ...> from < 表名 > where < 表达式 ...
- jpa无外键配置
在用jpa这种orm框架时,有时我们实体对象存在关联关系,但实际的业务场景可能不需要用jpa来控制数据库创建数据表之间的关联约束,这时我们就需要消除掉数据库表与表之间的外键关联.但jpa在处理建立外键 ...
- Redis工业生产应用场景
Redis应用场景 国内外三个不同领域巨头分享的Redis实战经验及使用场景 Redis的5个常见使用场景 Redis应用场景 Redis应用场景<张善友>
- php判断所在的客户端
//判断是否是手机 function is_mobile() { $agent = strtolower($_SERVER['HTTP_USER_AGENT']); $is_pc = (strpos( ...
- Idea配置CheckStyle
1.安装CheckStyle 2.配置idea_checks.xml 3.启用CheckStyle idea_checks.xml: <?xml version="1.0"? ...