How to increase the IP address represented as a string?

I have an IP address in char type Like char ip = "192.123.34.134" I want to increase the last value (134). Anyone how can I do this? I think I should convert it to an integer and then back, but unfortunately I do not know how to do this? :( I am using C ++.

Please help me!

Thanks kampi

+6
c ++ string ip-address
source share
7 answers

You can convert the IP address from a string to an integer using inet_addr , and then, after processing it, convert it back to a string using inet_ntoa .

For more information on how to use them, see the documentation for these functions.

Here's a little function that will do what you want:

 // NOTE: only works for IPv4. Check out inet_pton/inet_ntop for IPv6 support. char* increment_address(const char* address_string) { // convert the input IP address to an integer in_addr_t address = inet_addr(address_string); // add one to the value (making sure to get the correct byte orders) address = ntohl(address); address += 1; address = htonl(address); // pack the address into the struct inet_ntoa expects struct in_addr address_struct; address_struct.s_addr = address; // convert back to a string return inet_ntoa(address_struct); } 

Enable <arpa/inet.h> on * nix systems or <winsock2.h> on Windows.

+23
source share

Fast / Dirty!

 void increment(std::string& ip) { std::string::size_type dot = ip.find_last_of('.'); std::stringstream stream(ip.substr(dot+1)); int part = 0; stream >> part; part++; stream.str(""); stream.clear(); stream << part; ip.replace(dot+1, std::string::npos, stream.str()); } 
+4
source share
 int a,b,c,d; sscanf(str, "%d.%d.%d.%d", &a,&b,&c,&d); sprintf(str, "%d.%d.%d.%d\0", a,b,c,d+1); 
+3
source share

I would write a method that takes a string in this format.
Convert it to 4 integers. increment. (Important check range)
Then convert back to string.

If you want something more durable and reliable, a class representing an IP address. Then you maintain the class by manipulating as appropriate and convert to a string when necessary.

 #include <iostream> #include <istream> #include <sstream> #include <string> #include <stdexcept> class MyIp { struct Dot {}; struct Byte { Byte(unsigned char& val) :m_val(val) {} unsigned char& m_val; }; friend std::istream& operator>>(std::istream& str,MyIp::Dot const& d); friend std::istream& operator>>(std::istream& str,MyIp::Byte const& b); friend std::ostream& operator<<(std::ostream& str,MyIp const& ip); public: MyIp(std::string const& ip) { std::stringstream str(ip); str >> Byte(ad[0]) >> Dot() >> Byte(ad[1]) >> Dot() >> Byte(ad[2]) >> Dot() >> Byte(ad[3]); std::string leftover; if (str >> leftover) { throw std::runtime_error("InvalidIP: Long"); } } void inc(int index) { if ((index >= 0) && (index <=3)) { ++ad[index]; if (ad[index] == 0) { inc(index-1); } } } private: unsigned char ad[4]; }; std::istream& operator>>(std::istream& str,MyIp::Dot const& d) { char x = str.get(); if (x != '.') { throw std::runtime_error("Invalid IP: Dot"); } return str; } std::istream& operator>>(std::istream& str,MyIp::Byte const& b) { unsigned int val; str >> val; if (!str || val > 255) { throw std::runtime_error("Invalid IP: Val"); } b.m_val = static_cast<unsigned char>(val); return str; } std::ostream& operator<<(std::ostream& str,MyIp const& ip) { return str << static_cast<unsigned int>(ip.ad[0]) << "." << static_cast<unsigned int>(ip.ad[1]) << "." << static_cast<unsigned int>(ip.ad[2]) << "." << static_cast<unsigned int>(ip.ad[3]); } int main() { try { std::string ip("127.0.0.1"); MyIp addr(ip); std::cout << addr << "\n"; addr.inc(3); std::cout << addr << "\n"; } catch(std::exception const& e) { std::cout << "What: " << e.what() << "\n"; } } 
+2
source share

You can also use the location of the last ".". and from there to the end, to convert to int, bump it up 1, check the borders, and then convert them to a string and add to the base.

0
source share

This is probably not very cool, but it was interesting to think about.

Since the IP address space is 32 bits, you can write a function to convert IP addresses to unsigned 32-bit integers. You can then add or subtract 1 or as many as you want and convert back to an IP address. You do not have to worry about checking the range.

In the pseduo code for 192.123.34.134 you would do:

 int i = (192 << 24) + (123 << 16) + (34 << 8) + 134 

In general, for abcd:

 int i = (a << 24) + (b << 16) + (c << 8) + d 

Now change i as much as you want ( i++ , i+=10000 ) and go back:

 String ip = (i >> 24) + "." + ((i >> 16) mod 256) + "." + ((i >> 8) mod 256) + "." + (i mod 256); 

Sorry syntax - I could not write C ++ to save myself.

MK

0
source share

This site has eaten my tabs, so I will try again. I'm sure there is a library to do something similar, but this should work (assuming my syntax is not messed up) enough to convey the idea.

Pseudocode:

 char[] ipAddress= "192.123.34.134"; if (ipAddress[ipAddress.Length-1] == '9') { if(ipAddress[ipAddress.Length-2]=='9') { ipAddress[ipAddress.Length-1]='0'; ipAddress[ipAddress.Length-2]='0'; ipAddress[ipAddress.Length-3]=(char)ipAddress[ipAddress.Length-3]+1; } else { ipAddress[ipAddress.Length-2]=(char)ipAddress[ipAddress.Length-2]+1; ipAddress[ipAddress.Length-1]='0'; } } else { ipAddress[ipAddress.Length-1]=(char)ipAddress[ipAddress.Length-1]+1; } 
-2
source share

All Articles