开发者

converting string to network address

I have a Visual Studio 2008 C++ application where I would like to convert an IP address from a wide-character string in dotted-quad notation to an address (similar to inet_aton);

I'm doing this:

DWORD StringToAddress( const std::wstring& address )
{
    BYTE a = 0, b = 0, c = 0, d = 0;
    swscanf( address.c_str(), L"%u.%u.%u.%u", &a, &b, &c开发者_JAVA百科, &d );
    return d << 24 | c << 16 | b << 8 | a;
}

Unfortunately, when I give an address like 169.254.255.255 the third quad comes out of the swscanf as 0 and not 255.

Am I doing something wrong? Is there a good way to fix this?

Thanks, PaulH


Windows has inet_addr(), but it does not seem to support UNICODE:

#include <winsock2.h>

std::string address = "169.254.255.255";
unsigned long ip = inet_addr(address.c_str());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜