glenh
asked on
Assertion Fault on CIPAddressCtrl::SetAddress()
This may not be exactly the best approach to converting a string to a unsigned long for the SetAddress method but its the best I could come up with due to my lack of knowledge on bit conversions.
For some reason I am getting an assertion fault everytime I call SetAddress() even though it appears my conversion worked just fine. Here is a copy of the function that does the conversion. Is there something else I am possibly missing?
unsigned long EQLP_Utilities::strtodw(st ring ip) {
unsigned long ipaddress = 0;
unsigned long lbyte1, lbyte2, lbyte3, lbyte4;
string sbyte1, sbyte2, sbyte3, sbyte4;
string combinedip;
int patternptr = 0;
int startptr = 0;
int length = 0;
patternptr = ip.find('.', startptr);
sbyte1 = ip.substr(startptr,pattern ptr);
lbyte1 = atoi(sbyte1.c_str());
bitset<8> byte1(lbyte1);
startptr = patternptr + 1;
patternptr = ip.find('.', startptr);
sbyte2 = ip.substr(startptr,pattern ptr);
lbyte2 = atoi(sbyte2.c_str());
bitset<8> byte2(lbyte2);
startptr = patternptr + 1;
patternptr = ip.find('.', startptr);
sbyte3 = ip.substr(startptr,pattern ptr);
lbyte3 = atoi(sbyte3.c_str());
bitset<8> byte3(lbyte3);
startptr = patternptr + 1;
patternptr = ip.find('.', startptr);
sbyte4 = ip.substr(startptr,pattern ptr);
lbyte4 = atoi(sbyte4.c_str());
bitset<8> byte4(lbyte4);
combinedip.assign(byte1.to _string()) ;
combinedip.append(byte2.to _string()) ;
combinedip.append(byte3.to _string()) ;
combinedip.append(byte4.to _string()) ;
bitset<32> finalbits(combinedip);
ipaddress = finalbits.to_ulong();
return ipaddress;
}
For some reason I am getting an assertion fault everytime I call SetAddress() even though it appears my conversion worked just fine. Here is a copy of the function that does the conversion. Is there something else I am possibly missing?
unsigned long EQLP_Utilities::strtodw(st
unsigned long ipaddress = 0;
unsigned long lbyte1, lbyte2, lbyte3, lbyte4;
string sbyte1, sbyte2, sbyte3, sbyte4;
string combinedip;
int patternptr = 0;
int startptr = 0;
int length = 0;
patternptr = ip.find('.', startptr);
sbyte1 = ip.substr(startptr,pattern
lbyte1 = atoi(sbyte1.c_str());
bitset<8> byte1(lbyte1);
startptr = patternptr + 1;
patternptr = ip.find('.', startptr);
sbyte2 = ip.substr(startptr,pattern
lbyte2 = atoi(sbyte2.c_str());
bitset<8> byte2(lbyte2);
startptr = patternptr + 1;
patternptr = ip.find('.', startptr);
sbyte3 = ip.substr(startptr,pattern
lbyte3 = atoi(sbyte3.c_str());
bitset<8> byte3(lbyte3);
startptr = patternptr + 1;
patternptr = ip.find('.', startptr);
sbyte4 = ip.substr(startptr,pattern
lbyte4 = atoi(sbyte4.c_str());
bitset<8> byte4(lbyte4);
combinedip.assign(byte1.to
combinedip.append(byte2.to
combinedip.append(byte3.to
combinedip.append(byte4.to
bitset<32> finalbits(combinedip);
ipaddress = finalbits.to_ulong();
return ipaddress;
}
ASKER
Thanks jkr. I am still not 100% familiar with MFC and still learning. This is my first major go at it as I normally program mainly in MOTIF, Java and C++.
Go ahead and post as your answer and will award ya the points.
Go ahead and post as your answer and will award ya the points.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you would like to use a simpler method, the following will do the job for you:
/* convert address string to value */
int iLen = sizeof ( SOCKADDR_IN);
char* pszIP = "192.168.0.1";
SOCKADDR_IN addr;
addr.sin_family = AF_INET;
nRet = WSAStringToAddress (
pszIP, /* address string */
AF_INET, /* address family */
NULL, /* protocol info structure */
(LPSOCKADDR)&addr, /* socket address string */
&iLen); /* length of socket structure */
unsigned long ulIP = addr.in_addr.S_addr;