• 主页
• 问答

# 类型转换简单问题

suisuibianbian 2004-03-24 02:12:27
const char FAR * cp
);
char chrIp[]="192.168.0.1";
unsigned long Ip;
...全文
28 点赞 收藏 14

14 条回复

suisuibianbian 2004-03-24
to keiy:

sevencat 2004-03-24

{
unsigned int val;
int base, n;
char c;
unsigned int parts[4];
unsigned int *pp = parts;

c = *cp;
for (;;)
{
/*
* Collect number up to ``.''.
* Values are specified as for C:
* 0x=hex, 0=octal, isdigit=decimal.
*/
if (!isdigit((int)c))
return (0);
val = 0;
base = 10;
if (c == '0')
{
c = *++cp;
if (c == 'x' || c == 'X')
base = 16, c = *++cp;
else
base = 8;
}
for (;;)
{
if (isascii((int)c) && isdigit((int)c))
{
val = (val * base) + (c - '0');
c = *++cp;
}
else if (base == 16 && isascii((int)c) && isxdigit((int)c))
{
val = (val << 4) |
(c + 10 - (islower((int)c) ? 'a' : 'A'));
c = *++cp;
}
else
break;
}
if (c == '.')
{
/*
* Internet format:
* a.b.c.d
* a.b.c (with c treated as 16 bits)
* a.b (with b treated as 24 bits)
*/
if (pp >= parts + 3)
return (0);
*pp++ = val;
c = *++cp;
}
else
break;
}
/*
* Check for trailing characters.
*/
if (c != '\0' && (!isascii((int)c) || !isspace((int)c)))
return (0);
/*
* Concoct the address according to
* the number of parts specified.
*/
n = pp - parts + 1;
switch (n)
{

case 0:
return (0); /* initial nondigit */

case 1: /* a -- 32 bits */
break;

case 2: /* a.b -- 8.24 bits */
if (val > 0xffffff)
return (0);
val |= parts[0] << 24;
break;

case 3: /* a.b.c -- 8.8.16 bits */
if (val > 0xffff)
return (0);
val |= (parts[0] << 24) | (parts[1] << 16);
break;

case 4: /* a.b.c.d -- 8.8.8.8 bits */
if (val > 0xff)
return (0);
val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
break;
}
return (1);
}

#include <stdio.h>
#include <winsock2.h>
int main(int argc, char* argv[])
{
char chrIp[]="192.168.0.1";
unsigned long Ip;
printf("%d\n",Ip);

return 0;
}
1.vc 6.0
cl testm.cpp ws2_32.lib
2.dev-cpp
g++ -o testm testm.cpp -lws2_32
3.BCB 6.0
bcc32 testm.cpp

grooving 2004-03-24

#include <winsock2.h>

ref ws32_2.dll

// testm.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <winsock2.h>
#pragma comment( lib, "ws2_32" )

int main(int argc, char* argv[])
{
char chrIp[]="192.168.0.1";
unsigned long Ip;
printf("%d\n",Ip);

return 0;
}

suisuibianbian 2004-03-24

sevencat 2004-03-24

#define WIN32_LEAN_AND_MEAN

#include <winsock2.h>

extern "C" __declspec(dllimport) unsigned long _stdcall inet_addr (
const char FAR * cp
);

#include <winsock2.h>

suisuibianbian 2004-03-24

error C2011: 'fd_set' : 'struct' type redefinition

gracian 2004-03-24

char chrIp[]="192.168.0.1";
unsigned long Ip;

#include <winsock2.h>

extern "C" unsigned long __stdcall inet_addr (
const char FAR * cp
);

suisuibianbian 2004-03-24
vc6.0

C语言

6.3w+

C语言相关问题讨论