简体   繁体   中英

What data type should I use to store an IP Address?

I suddenly just thought of this and got stuck deciding which data type should I use to store an IP Address?

I have thought of NSString ; But if I would need the last digit for identifications, should I use float or double? And that is also another problem, since when can float or double have more than 1 decimal point?

I am probably asking the question wrongly, because I really don't know how to ask this.

The IP Address comes from an XML format <IP>192.168.1.1</IP> . Any idea how I should do this?

Use a string. You don't need to perform arithmetic on the IP do you?

It is typical to store IP addresses as strings, or arrays of integers. Another option is to store it as a 32 bit integer. It really comes down to what you want to do.

I would use NSString . If you need to get it piece by piece, use:

NSArray *pieces = [ipAddress componentsSeparatedByString: @"."];

Use NSString . You are not going to do arithmetic with it. If you need the separate components you can use NSArray , but NSString will serve you well for just storing the IP address.

In response to your needs, you can always obtain the last character in your string using the NSString method:

NSString *lastCharacter = [ip_string substringFromIndex: [ip_string length] - 1];

Where ip_string is the string holding the IP address.

Edit in response to comment:

Logan's code is storing each element in the IP address separated by a period into an array. So if the IP address is 192.168.1.1, the array will equal (192, 168, 1, 1).

My code is storing the entire IP address in a string, and then obtaining the last character in that string. [ip_string substringFromIndex: [ip_string length] - 1] is just obtaining the last character in the string containing the IP address. The last character can be found at minus one character.

So if the IP address is 192.168.1.1, the lastCharacter string will just contain the number 1 .

I suggested that code because you stated that you needed to do something with the last character in your IP address string, and my code shows how you can obtain the last character.

192.168.1.1 is considered the default IP for numerous home high-speed wireless routers. It had been initially utilized by Linksys and yet has been seen used in a number of other home network products including some of those manufactured by Netgear and also Westell among others.

Even though IP address stands out as the default ip for a lot of high speed broadband wireless routers, this does not essentially has to be. A large number of producers set the default IP address to 192.168.1.1 as a way to market a standard precessing conditions and to make it simpler for very first time clients to setup their own networking systems simply and efficiently.

May only Linksys as well as other wireless routers operate using the 192.168.1.1 IP?

Certainly no, given that 192.168.1.1 is definitely a non-public IPv4 address, any type of laptop or computer, modem, switch, or another web system might be devised to work with this unique IP. Nonetheless, it's not in most cases advisable because there are lots of products which default to 192.168.1.1 which in turn interaction issues can occur soon after from many different products utilizing the same IP. It's also really important to consider that a single network equipment might have just one single Ip, if you own numerous units using private IP address , basically at least one has to be adjusted to an alternative location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM