Convert CString
-
Hi Guys I have to convert a IP-Address which is a CString to an UInt, but i have no clue how to do this. Also i must convert a Port CString to an UInt. MFG RedDragon2kx Unix and C are the ultimate computer viruses.
-
Hi Guys I have to convert a IP-Address which is a CString to an UInt, but i have no clue how to do this. Also i must convert a Port CString to an UInt. MFG RedDragon2kx Unix and C are the ultimate computer viruses.
don't you use a class that use ip addresses, and in which some function take a string (either char* or std::string or CString...) ? else, you might write your own function that test first the validity of the address, and then, extract it to a UInt...
TOXCCT >>> GEII power
[toxcct][VisualCalc] -
Hi Guys I have to convert a IP-Address which is a CString to an UInt, but i have no clue how to do this. Also i must convert a Port CString to an UInt. MFG RedDragon2kx Unix and C are the ultimate computer viruses.
You can sscanf the string like so:
CString sIPAddr;
UINT nByte1, nByte2, nByte3, nByte4;
UINT nPort;
DWORD dwIPAddr;
int nArgs = sscanf( (LPCTSTR)sIPAddr, "%d.%d.%d.%d:%d",
&nByte1, &nByte2, &nByte3, &nByte4, &nPort );
if( nArgs != 5 )
{
// Didn't extract 5 valid numeric arguments...take action!
}
else
{
// Validate that the nByte are valid 0-255.
...
...
// Pack the IPAddress into the DWORD.
dwIPAddr = (nByte1 << 24) + (nByte2 << 16) + (nByte3 << 8) + nByte4;
}You may want to verify my byte ordering (nByte4 may be the high byte) when building the dwIPAddr, but this should do the trick. Bob Ciora
-
You can sscanf the string like so:
CString sIPAddr;
UINT nByte1, nByte2, nByte3, nByte4;
UINT nPort;
DWORD dwIPAddr;
int nArgs = sscanf( (LPCTSTR)sIPAddr, "%d.%d.%d.%d:%d",
&nByte1, &nByte2, &nByte3, &nByte4, &nPort );
if( nArgs != 5 )
{
// Didn't extract 5 valid numeric arguments...take action!
}
else
{
// Validate that the nByte are valid 0-255.
...
...
// Pack the IPAddress into the DWORD.
dwIPAddr = (nByte1 << 24) + (nByte2 << 16) + (nByte3 << 8) + nByte4;
}You may want to verify my byte ordering (nByte4 may be the high byte) when building the dwIPAddr, but this should do the trick. Bob Ciora
Thx. That fixed my problem. :doh: ;) Unix and C are the ultimate computer viruses.
-
Thx. That fixed my problem. :doh: ;) Unix and C are the ultimate computer viruses.
For future reference, take a look at the inet_addr function in the MSDN library. I can't believe there isn't built in global c runtime functions for parsing an IP address in this day and age. But if you are programming with MFC or windows, you can use this windows function. I hate the sscanf method myself. It seems wonderful on paper but there seem to be all kinds of weird issues when I try to use it. It never worked well for me as a validation tool for any kind of number. I always use the cruntime methods such as strtod for taking strings and validating them as actual numbers and converting them into an int value (for instance). Unfortunately, I don't see a c-run time method for IP addresses that would be more portable. Since I am a windows programmer, I have to settle for the windows libraries to provide this capability. Shawn