怎么把这段字符串里的数字提出来再赋值给2个整型变量?

cBeginner 2003-04-22 10:13:10
char iparranger[] = "140.134.056.100-169.199.165.032";

int ip1 = 0;

int ip2 = 0;

怎么把ip1的值变成140134056100,ip2的值变成169199165032 ?
...全文
116 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhouzhaohan 2003-04-25
  • 打赏
  • 举报
回复
1.你的逻辑有点混乱。
2.你的ipRange 和location数组都会产生越界。
不用这么麻烦的。
void main()
{
#define TEMP_LEN 100
char data[] = "211.090.143.001-211.090.145.255@江苏省常州用户";
char ipRange[TEMP_LEN] = " ";
char location[TEMP_LEN] = " ";
char *temp;

temp=strchr(data,'@');
strncpy(ipRange,data,temp-data);
strcpy(location,temp+1);
printf("%s %s",ipRange,location);
}
chegtion 2003-04-24
  • 打赏
  • 举报
回复
DWORD 最大值4 294 967 295
最好使用double型
cBeginner 2003-04-24
  • 打赏
  • 举报
回复
第一行应该是char data[] = "211.090.143.001-211.090.145.255@江苏省常州用户";
刚才写错了。
cBeginner 2003-04-24
  • 打赏
  • 举报
回复
char data[] = "211.090.143.001-211.090.145.255:江苏省常州用户";
char ipRange[] = " ";
char location[] = " ";

for(int i = 0; i <= strlen(data); i++){
// COPY IP ADDRESS
if(data[i] != '@'){
ipRange[i] = data[i];
cout << ipRange[i];
}
else{
// COPY LOCATION NAME
cout << "\nlocation: ";
for(int namesz = i+1; namesz <= strlen(data)-1; namesz++){
location[namesz] = data[namesz];
cout << location[namesz];
}
i = strlen(data) + 2;
// 用dev-c,这里跳不出for循环,用bc可以跳出循环,为什么?
}
}
cout << "\nlocation: " << location; //为什么这个location出了for循环就变成空的了
//system("PAUSE");
zhouzhaohan 2003-04-23
  • 打赏
  • 举报
回复
就挺好的,把ip都转换成long,然后就用一般的比较就可以。事实上,你用strcmp也可以,因为你给出的格式很整齐,每个点前面都有三个字符,而且碰巧数字越大,它的ascii的值也越大。
cBeginner 2003-04-23
  • 打赏
  • 举报
回复
我还想问一下,我在网上下载的IP地址数据文本文件,是通过什么逻辑来查询ip对应地理位置的?我想用他们的数据文件,自己写一个玩。


比如文本文件里的数据:

061.008.000.000__061.008.031.255__澳大利亚______
061.010.200.000__061.010.210.255__香港______
061.018.235.000__061.018.235.255__香港______
061.112.000.000__061.112.255.255__日本______
061.117.030.000__061.117.035.255__日本__东京____
061.128.099.000__061.128.100.255__新疆__乌鲁木齐169专线用户____
061.128.102.000__061.128.102.255__新疆__阿勒泰169专线用户____

我想输入一个ip地址,来判断是否在2个ip地址之间,如果是的话就输出后面的地理位置。

有没有其他更好的逻辑方法?
zhouzhaohan 2003-04-23
  • 打赏
  • 举报
回复
就是普通函数,不属于任何类
zhouzhaohan 2003-04-23
  • 打赏
  • 举报
回复
inet_addr
The Windows Sockets inet_addr function converts a string containing an (Ipv4) Internet Protocol dotted address into a proper address for the IN_ADDR structure.

unsigned long inet_addr (
const char FAR * cp
);

Parameters
cp
[in] A null-terminated character string representing a number expressed in the Internet standard ".'' (dotted) notation.
Remarks
The inet_addr function interprets the character string specified by the cp parameter. This string represents a numeric Internet address expressed in the Internet standard ".'' notation. The value returned is a number suitable for use as an Internet address. All Internet addresses are returned in IP's network order (bytes ordered from left to right).

Internet Addresses

Values specified using the ".'' notation take one of the following forms:

a.b.c.d a.b.c a.b a

When four parts are specified, each is interpreted as a byte of data and assigned, from left to right, to the four bytes of an Internet address. When an Internet address is viewed as a 32-bit integer quantity on the Intel architecture, the bytes referred to above appear as "d.c.b.a''. That is, the bytes on an Intel processor are ordered from right to left.

The parts that make up an address in "." notation can be decimal, octal or hexidecimal as specified in the C language. Numbers that start with "0x" or "0X" imply hexidecimal. Numbers that start with "0" imply octal. All other numbers are interpreted at decimal.

"4.3.2.16" decimal
"004.003.002.020" octal
"0x4.0x3.0x2.0x10" hexidecimal
"4.003.002.0x10" mix


Note The following notations are only used by Berkeley, and nowhere else on the Internet. In the interests of compatibility with their software, they are supported as specified.

When a three part address is specified, the last part is interpreted as a 16-bit quantity and placed in the right most two bytes of the network address. This makes the three part address format convenient for specifying Class B network addresses as "128.net.host''.

When a two part address is specified, the last part is interpreted as a 24-bit quantity and placed in the right most three bytes of the network address. This makes the two part address format convenient for specifying Class A network addresses as "net.host''.

When only one part is given, the value is stored directly in the network address without any byte rearrangement.

Return Values
If no error occurs, inet_addr returns an unsigned long value containing a suitable binary representation of the Internet address given. If the string in the cp parameter does not contain a legitimate Internet address, for example if a portion of an "a.b.c.d" address exceeds 255, inet_addr returns the value INADDR_NONE.

QuickInfo
Windows NT: Yes
Windows: Yes
Windows CE: Use version 1.0 and later.
Header: Declared in winsock2.h.
Import Library: Link with ws2_32.lib.
cBeginner 2003-04-23
  • 打赏
  • 举报
回复
inet_addr()是哪个类里的?
kc11 2003-04-23
  • 打赏
  • 举报
回复
太好了,我也有做这方面东西的想法,给了我很多提示。不错不错
binggobaby 2003-04-23
  • 打赏
  • 举报
回复
我也长了点见识
呵呵
谢谢楼主
cBeginner 2003-04-23
  • 打赏
  • 举报
回复
可不可以不去掉点,直接比较?比如“061.128.102.000”和“061.128.102.255”
langzi8818 2003-04-22
  • 打赏
  • 举报
回复
!
zhouzhaohan 2003-04-22
  • 打赏
  • 举报
回复
你是认真的吗?你两个数字都超过了32位整形的最大值。
你应该把没一个.内的数字转换成十六进制,放到long相应的字节上去。
你可以这么做。
char iparranger[] = "140.134.056.100-169.199.165.032";
char *pos=strchr(iparranger,'-');
long ip1,ip2;
*pos='\0';
ip1=inet_addr(iparranger);
ip2=inet_addr(pos+1);
如果你不希望破坏iparranger的内容,可以把字符串先拷贝一份。
另外是不是起名叫iprange更好一点。:)
间谍 2003-04-22
  • 打赏
  • 举报
回复
楼上的方法好,不知道iostreams有没有这么灵活的方法
arfi 2003-04-22
  • 打赏
  • 举报
回复
上面写错了,scanf应该为:
sscanf(iparranger, "%d.%d.%d.%d%*c%d.%d.%d.%d", &p1[3], &p1[2], &p1[1], &p1[0], &p2[3], &p2[2], &p2[1], &p2[0]);
arfi 2003-04-22
  • 打赏
  • 举报
回复
unsigned long ip1, ip2;
unsigned char *p1 = &ip1;
unsigned char *p2 = &ip2;

scanf("%d.%d.%d.%d%*c%d.%d.%d.%d", &p1[3], &p1[2], &p1[1], &p1[0], &p2[3], &p2[2], &p2[1], &p2[0]);

未调试
suguanqun 2003-04-22
  • 打赏
  • 举报
回复
同意一楼!
xyzguan 2003-04-22
  • 打赏
  • 举报
回复
遍历一遍就出来了
判断每个iparranger[i]是什么,是数字就放到ipl里,具体算法ipl=ipl*10+ipl

至于-前后的问题,不用我说了吧?
leialen 2003-04-22
  • 打赏
  • 举报
回复
:)
up !-> zhouzhaohan()
加载更多回复(1)

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧