I'm trying to parse and show the MAC addresses of my machine for a license program I'm making, but show only those with a valid IP address. I already got the IPs/MACs but I'm unable to see which one is valid and which one is not.
Here's my code:
void FormSerial::PopCB(QStringList CB){
for (int i = 0; i<CB.size(); i++){
if(CB.at(i) == "0.0.0.0"){
Log("verdadero " + CB.at(i+1));
widget.Mac_ComboBox->addItem(CB.at(i+1));
}else{
Log("Falso " + CB.at(i));
}
}
}
If I do it this way, it shows me those who have no valid IP address, but I want only those with a valid IP. If I change the ==
to !=
inside the if
, I get nothing.
I have the data like this:
CB = "0.0.0.0", "42:45:65:25:89:45", "196.568.68.49", "45:98:75:85:15:85"
Any idea?
Try this instead:
Just be aware that this code only handles IPv4 IP address, but machines can have IPv6 IPs as well. So you may need to update the loop to handle that as well, eg:
Note that IPv6 IPs are much harder to parse than IPv4, as they have many different possible representations, so you are better off using an API/library that knows how to parse IP address strings, such as
inet_pton()
orgetaddrinfo()
, eg: