This has been asked and answered multiple times. It is just a question of finding the correct search term. I used "c++ parse ip string" and got a lot of useful results:
C++ how to convert ip address to bytes? - Stack Overflow[
^]
c - parsing ip adress string in 4 single bytes - Stack Overflow[
^]
Once you have choosen a parsing method you must observe that your string is
CString
(
TCHAR *
). Therefore, use the
_t*
versions of the C standard library (e.g.
_stscanf
). Because most string to integer conversions return an integer, you must cast them to bytes when assigning.
Example using
sscanf
/
_stscanf
:
unsigned short a, b, c, d;
_stscanf(ip_address.GetString(), _T("%hu.%hu.%hu.%hu"), &a, &b, &c, &d);
unsigned char ipaddr[4];
ipaddr[0] = static_cast<unsigned char>(a);
ipaddr[1] = static_cast<unsigned char>(b);
ipaddr[2] = static_cast<unsigned char>(c);
ipaddr[3] = static_cast<unsigned char>(d);