PaulH
PaulH

Reputation: 7843

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, &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

Upvotes: 2

Views: 2739

Answers (1)

Fr&#233;d&#233;ric Hamidi
Fr&#233;d&#233;ric Hamidi

Reputation: 262919

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());

Upvotes: 1

Related Questions