开发者

converting string to network address

开发者 https://www.devze.com 2023-02-16 03:22 出处:网络
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 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

精彩评论

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