将ip地址转换为long值 [英] Convert an ip address to long value
问题描述
我正在尝试编写一个函数,将字符串char * ip中的
转换为其长值等效的ipv4地址。这就是我现在所拥有的b $ b,但我似乎无法让它工作。
#include< string.h>
#include< stdio.h>
/ *将ipv4地址转换为长整数* /
/ *" 192.168.1.1" - > 3232235777 * /
unsigned long iptol(char * ip){
unsigned char o1,o2,o3,o4; / * 4个字符* /
char tmp [13] =" 000000000000 \ 0" ;;
short i = 11; / *目前的指数在tmp * /
短j =(strlen(ip) - 1);
do {
if((ip) [--j] ==''。'')){
i - =(i%3);
}
else {
tmp [ - i] = ip [j];
}
} while(i> -1);
o1 =(tmp [0] * 100)+(tmp [1] * 10)+ tmp [2];
o2 =(tmp [3] * 100)+( tmp [4] * 10)+ tmp [5];
o3 =(tmp [6] * 100)+(tmp [7] * 10)+ tmp [8];
o4 =(tmp [9] * 100)+(tmp [10] * 10)+ tmp [11];
返回(o1 * 16777216)+(o2 * 65536)+ (o3 * 256)+ o4;
}
int main(无效){
char * ip =" 124.15 .22.102" ;;
iptol(ip);
}
有什么建议吗?也许还有更好的办法吗?
-kyle
kyle .tk写道:我正在尝试编写一个函数来将字符串char * ip中保存的ipv4地址转换为其长值等价物。这就是我现在所拥有的,但我似乎无法让它发挥作用。
#include< string.h>
#include< stdio .h>
/ *将ipv4地址转换为长整数* /
/ *" 192.168.1.1" - > 3232235777 * /
unsigned long iptol(char * ip){
unsigned char o1,o2,o3,o4; / * 4个字符* /
char tmp [13] =" 000000000000\0" ;;
short i = 11; / *当前指数在tmp * /
短j =(strlen(ip) - 1);
做{
if(([[[j] ==''''''' )){
我 - =(i%3);
}
其他{
tmp [ - i] = ip [j];
}
} while(i> -1);
o1 =(tmp [0] * 100)+(tmp [1] * 10)+ tmp [2];
o2 =(tmp [ 3] * 100)+(tmp [4] * 10)+ tmp [5];
o3 =(tmp [6] * 100)+(tmp [7] * 10)+ tmp [8];
o4 =(tmp [9] * 100)+(tmp [10] * 10)+ tmp [11];
返回(o1 * 16777216)+(o2 * 65536)+(o3 * 256) + o4;
}
int main(void){
char * ip =" 124.15.22.102" ;;
iptol(ip);
}
有什么建议吗?也许有更好的方法来做到这一点?
现在太懒了;考虑
#include< string.h>
#include< stdlib.h>
#include< stdio .h>
#define NUM_OCTETTS 4
int iptoul(const char * ip,unsigned long * plong)
{
char * next = NULL;
const char * curr = ip;
unsigned long tmp;
int i,err = 0;
* plong = 0;
for(i = 0; i< NUM_OCTETTS; i ++){
tmp = strtoul(curr,& next,10);
if(tmp> = 256
||(tmp == 0& ;& next == curr))
{
err ++;
break;
}
* plong =(* plong<< 8)+ tmp;
curr = next + 1;
}
>
if(错误){
返回1;
}
else {
返回0 ;
}
}
int main(无效)
{
const char * ip =" 124.15.22.102" ;;
unsigned long ret;
if(0 == iptoul(ip,& ret))
printf("%s - > %lu \ n",ip,ret);
返回0;
}
干杯
Michael
-
电子邮箱:我的是/ at / gmx / dot / de地址。
Michael Mair写道:kyle.tk写道:
我正在尝试编写一个函数来转换一个ipv4地址在字符串char * ip中保持
到它的long值等价物。这就是我现在所拥有的,但我似乎无法让它发挥作用。
#include< string.h>
#include< stdio .h>
/ *将ipv4地址转换为长整数* /
/ *" 192.168.1.1" - > 3232235777 * /
unsigned long iptol(char * ip){
unsigned char o1,o2,o3,o4; / * 4个字符* /
char tmp [13] =" 000000000000\0" ;;
short i = 11; / *当前指数在tmp * /
短j =(strlen(ip) - 1);
做{
if(([[[j] ==''''''' )){
我 - =(i%3);
}
其他{
tmp [ - i] = ip [j];
}
} while(i> -1);
o1 =(tmp [0] * 100)+(tmp [1] * 10)+ tmp [2];
o2 =(tmp [ 3] * 100)+(tmp [4] * 10)+ tmp [5];
o3 =(tmp [6] * 100)+(tmp [7] * 10)+ tmp [8];
o4 =(tmp [9] * 100)+(tmp [10] * 10)+ tmp [11];
返回(o1 * 16777216)+(o2 * 65536)+(o3 * 256) + o4;
}
int main(void){
char * ip =" 124.15.22.102" ;;
iptol(ip);
}
有什么建议吗?也许有更好的方法可以做到这一点?
现在太懒了;考虑
#include< string.h>
#include< stdlib.h>
#include< stdio.h>
#定义NUM_OCTETTS 4
int iptoul(const char * ip,unsigned long * plong)
{char / next = NULL;
const char * curr = ip;
unsigned long tmp;
int i,err = 0;
* plong = 0;
for(i = 0; i< NUM_OCTETTS; i ++){
tmp = strtoul(curr,& next,10);
if(tmp> = 256
||(tmp == 0&& next == curr))
{
错误++;
休息;
}
忘了显而易见的一个:
if(* next !=''。''&& i!=(NUM_OCTETTS-1)){
err ++;
break;
} * plong =(* plong<< 8)+ tmp;
curr = next + 1;
}
}
其他{
返回0;
}
}
int main(无效)
{
const char * ip =" 124.15.22.102" ;;
unsigned long ret;
if(0 == iptoul(ip,& ret))
printf("%s - > %lu \ n",ip,ret);
返回0;
}
欢呼声
Michael
-
电子邮箱:我的是/ at / gmx / dot / de地址。
kyle.tk写于01/31/06 15:27,:我正在尝试编写一个函数来转换所持有的ipv4地址
在字符串char * ip中它的long值等价。这就是我现在所拥有的,但我似乎无法让它发挥作用。
#include< string.h>
#include< stdio .h>
/ *将ipv4地址转换为长整数* /
/ *" 192.168.1.1" - > 3232235777 * /
unsigned long iptol(char * ip){
unsigned char o1,o2,o3,o4; / * 4个字符* /
char tmp [13] =" 000000000000\0" ;;
short i = 11; / *当前指数在tmp * /
短j =(strlen(ip) - 1);
做{
if(([[[j] ==''''''' )){
我 - =(i%3);
}
其他{
tmp [ - i] = ip [j];
}
} while(i> -1);
o1 =(tmp [0] * 100)+(tmp [1] * 10)+ tmp [2];
o2 =(tmp [ 3] * 100)+(tmp [4] * 10)+ tmp [5];
o3 =(tmp [6] * 100)+(tmp [7] * 10)+ tmp [8];
o4 =(tmp [9] * 100)+(tmp [10] * 10)+ tmp [11];
返回(o1 * 16777216)+(o2 * 65536)+(o3 * 256) + o4;
}
int main(void){
char * ip =" 124.15.22.102" ;;
iptol(ip);
}
有什么建议吗?也许还有更好的方法吗?
您可以使用sscanf()。不幸的是,说服sscanf()拒绝诸如123.0.0.-1
或之类的东西是很困难的。 127. 0. 0. 1。
另一种可能性是使用isdigit()来检查每个字段的第一个字符,然后检查strtoul( )
转换它,然后检查strtoul()停在''。''
(或最后一次''\ 0'') 。您也可以检查
停止点距离开始是不是太远,所以
拒绝000000000000127.0.0.000000000000000001。
-
Er ********* @ sun。 com
I am trying to write a function to convert an ipv4 address that is held
in the string char *ip to its long value equivalent. Here is what I
have right now, but I can''t seem to get it to work.
#include <string.h>
#include <stdio.h>
/* Convert an ipv4 address to long integer */
/* "192.168.1.1" --> 3232235777 */
unsigned long iptol(char *ip){
unsigned char o1,o2,o3,o4; /* The 4 ocets */
char tmp[13] = "000000000000\0";
short i = 11; /* Current Index in tmp */
short j = (strlen(ip) - 1);
do {
if ((ip[--j] == ''.'')){
i -= (i % 3);
}
else {
tmp[--i] = ip[j];
}
} while (i > -1);
o1 = (tmp[0] * 100) + (tmp[1] * 10) + tmp[2];
o2 = (tmp[3] * 100) + (tmp[4] * 10) + tmp[5];
o3 = (tmp[6] * 100) + (tmp[7] * 10) + tmp[8];
o4 = (tmp[9] * 100) + (tmp[10] * 10) + tmp[11];
return (o1 * 16777216) + (o2 * 65536) + (o3 * 256) + o4;
}
int main(void){
char *ip = "124.15.22.102";
iptol(ip);
}
Any suggestions? Maybe there is a better way to do this?
-kyle
kyle.tk wrote:I am trying to write a function to convert an ipv4 address that is held
in the string char *ip to its long value equivalent. Here is what I
have right now, but I can''t seem to get it to work.
#include <string.h>
#include <stdio.h>
/* Convert an ipv4 address to long integer */
/* "192.168.1.1" --> 3232235777 */
unsigned long iptol(char *ip){
unsigned char o1,o2,o3,o4; /* The 4 ocets */
char tmp[13] = "000000000000\0";
short i = 11; /* Current Index in tmp */
short j = (strlen(ip) - 1);
do {
if ((ip[--j] == ''.'')){
i -= (i % 3);
}
else {
tmp[--i] = ip[j];
}
} while (i > -1);
o1 = (tmp[0] * 100) + (tmp[1] * 10) + tmp[2];
o2 = (tmp[3] * 100) + (tmp[4] * 10) + tmp[5];
o3 = (tmp[6] * 100) + (tmp[7] * 10) + tmp[8];
o4 = (tmp[9] * 100) + (tmp[10] * 10) + tmp[11];
return (o1 * 16777216) + (o2 * 65536) + (o3 * 256) + o4;
}
int main(void){
char *ip = "124.15.22.102";
iptol(ip);
}
Any suggestions? Maybe there is a better way to do this?
Too lazy to look right now; consider
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define NUM_OCTETTS 4
int iptoul (const char *ip, unsigned long *plong)
{
char *next = NULL;
const char *curr = ip;
unsigned long tmp;
int i, err = 0;
*plong = 0;
for (i = 0; i < NUM_OCTETTS; i++) {
tmp = strtoul(curr, &next, 10);
if (tmp >= 256
|| (tmp == 0 && next == curr))
{
err++;
break;
}
*plong = (*plong << 8) + tmp;
curr = next + 1;
}
if (err) {
return 1;
}
else {
return 0;
}
}
int main (void)
{
const char *ip = "124.15.22.102";
unsigned long ret;
if (0 == iptoul(ip, &ret))
printf("%s -> %lu\n", ip, ret);
return 0;
}
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Michael Mair wrote:kyle.tk wrote:I am trying to write a function to convert an ipv4 address that is held
in the string char *ip to its long value equivalent. Here is what I
have right now, but I can''t seem to get it to work.
#include <string.h>
#include <stdio.h>
/* Convert an ipv4 address to long integer */
/* "192.168.1.1" --> 3232235777 */
unsigned long iptol(char *ip){
unsigned char o1,o2,o3,o4; /* The 4 ocets */
char tmp[13] = "000000000000\0";
short i = 11; /* Current Index in tmp */
short j = (strlen(ip) - 1);
do {
if ((ip[--j] == ''.'')){
i -= (i % 3);
}
else {
tmp[--i] = ip[j];
}
} while (i > -1);
o1 = (tmp[0] * 100) + (tmp[1] * 10) + tmp[2];
o2 = (tmp[3] * 100) + (tmp[4] * 10) + tmp[5];
o3 = (tmp[6] * 100) + (tmp[7] * 10) + tmp[8];
o4 = (tmp[9] * 100) + (tmp[10] * 10) + tmp[11];
return (o1 * 16777216) + (o2 * 65536) + (o3 * 256) + o4;
}
int main(void){
char *ip = "124.15.22.102";
iptol(ip);
}
Any suggestions? Maybe there is a better way to do this?
Too lazy to look right now; consider
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define NUM_OCTETTS 4
int iptoul (const char *ip, unsigned long *plong)
{
char *next = NULL;
const char *curr = ip;
unsigned long tmp;
int i, err = 0;
*plong = 0;
for (i = 0; i < NUM_OCTETTS; i++) {
tmp = strtoul(curr, &next, 10);
if (tmp >= 256
|| (tmp == 0 && next == curr))
{
err++;
break;
}
Forgot the obvious one:
if (*next != ''.'' && i != (NUM_OCTETTS-1)) {
err++;
break;
} *plong = (*plong << 8) + tmp;
curr = next + 1;
}
if (err) {
return 1;
}
else {
return 0;
}
}
int main (void)
{
const char *ip = "124.15.22.102";
unsigned long ret;
if (0 == iptoul(ip, &ret))
printf("%s -> %lu\n", ip, ret);
return 0;
}
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
kyle.tk wrote On 01/31/06 15:27,:I am trying to write a function to convert an ipv4 address that is held
in the string char *ip to its long value equivalent. Here is what I
have right now, but I can''t seem to get it to work.
#include <string.h>
#include <stdio.h>
/* Convert an ipv4 address to long integer */
/* "192.168.1.1" --> 3232235777 */
unsigned long iptol(char *ip){
unsigned char o1,o2,o3,o4; /* The 4 ocets */
char tmp[13] = "000000000000\0";
short i = 11; /* Current Index in tmp */
short j = (strlen(ip) - 1);
do {
if ((ip[--j] == ''.'')){
i -= (i % 3);
}
else {
tmp[--i] = ip[j];
}
} while (i > -1);
o1 = (tmp[0] * 100) + (tmp[1] * 10) + tmp[2];
o2 = (tmp[3] * 100) + (tmp[4] * 10) + tmp[5];
o3 = (tmp[6] * 100) + (tmp[7] * 10) + tmp[8];
o4 = (tmp[9] * 100) + (tmp[10] * 10) + tmp[11];
return (o1 * 16777216) + (o2 * 65536) + (o3 * 256) + o4;
}
int main(void){
char *ip = "124.15.22.102";
iptol(ip);
}
Any suggestions? Maybe there is a better way to do this?
You could use sscanf(). Unfortunately, it''s hard
to persuade sscanf() to reject things like "123.0.0.-1"
or " 127. 0. 0. 1".
Another possibility would be to use isdigit() to
check the first character of each field and then strtoul()
to convert it, then check that strtoul() stopped on a ''.''
(or on a ''\0'' the final time). You might also check that
the stopping point wasn''t too far from the start, so as
to reject "000000000000127.0.0.000000000000000001".
--
Er*********@sun.com
这篇关于将ip地址转换为long值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!