Convert string to IP address format
Asked Answered
F

5

5

I'm reading IP address numbers from database in a int format but I want to show them in IP format like 000.000.000.000

Is it possible using the String.Format method?

For e.g.:

string str = String.Format("{0:#,###,###.##}", ips); 
Fultz answered 22/2, 2011 at 6:4 Comment(1)
what language and what is your ips an array of ints or just one big number. Give an example of ips.Fulbert
T
13

If you use the IPAddress object in the System.Net namespace to parse your address in as follows:

IPAddress ip = IPAddress.Parse(IPStringHere);

then use the "ToString()" method on that object:

string outputString = ip.ToString();

you can get a string that shows the IP address provided in the format you require using integers and full stops.

The IPAddress parsing routine will take a string IP address and it will also take an int/long int format address too allowing you to parse both formats.

As a bonus if your use "TryParse" and put your conversion in an if block, you can also test to see if the input format results in a correct IP address before returning the string, handy for input validation.

Tutorial answered 21/1, 2013 at 3:23 Comment(3)
You know that the answer has nothing to do with the question right? I know 4 years too late but it is still not an answer to the question.Closelipped
Down-vote because all this does is convert a string to an IP address structure, the OP explicitly asked for a string format output in order to display the IP address (Exactly the same reason I ended up here through google)Charger
Down-vote removed and I edited the question instead.Charger
B
2

Are these 32-bit integers that each represent the entire IP address? If so...

IPAddress ip = new IPAddress((long)ips);
return ip.ToString();

(I don't know why that constructor takes a long, it'll throw an exception if you exceed the range of a UInt32, so a UInt32 would be more appropriate in my opinion.)

Beckibeckie answered 22/2, 2011 at 6:19 Comment(1)
Using the Usomething (or their alias usomething like uint) types is frowned in .NET. It isn't CLS compliamnt, because some languages don't distinguish between signed and unsigned (and have only, normally, the signed version). IPAddress is a .NET framework class, so it must be compatible with all the languages, so no UInt32 and yes to longSurfperch
H
2

I came looking for the same answer .. but seeing as there are non I write my self.

private string padIP(string ip)
{
    string output = string.Empty;
    string[] parts = ip.Split('.');
    for (int i = 0; i < parts.Length; i++)
    {
        output += parts[i].PadLeft(3, '0');
        if (i != parts.Length - 1)
            output += ".";
    }
    return output;
}

edit: ah i see in int format .. never mind, not sure how you would do that....

Harrow answered 17/2, 2012 at 14:22 Comment(0)
F
0

Something like this?
.Net 4.0

string str = string.Join(".", ips.Select(x => x.ToString()))

Earlier

string str = string.Join(".", ips.Select(x => x.ToString()).ToArray())

This says, join a string[] with . in between. The array is made up of ips, converted to string using ToString() and turned into a string[] using ToArray()

Fulbert answered 22/2, 2011 at 6:13 Comment(2)
IN C# 4.0 they added a IEnumerable version of join, so you can delete the "ToArray()".Surfperch
nice! i'll update with both version, OP didnt specify which version of .netFulbert
S
0

If it's an array:

string str = String.Format("{0:###}.{1:###}.{2:###}.{3:###}", ips[0], ips[1], ips[2], ips[3]);
Surfperch answered 22/2, 2011 at 6:14 Comment(4)
but i need to add dot with a single return(nvarchar type) of query's output like 111111111111 toFultz
What type is your ips? Is it an array of int, or is it a string or what?Surfperch
m not using any array there for just reading data from reader like string ips = dr["block_ip"].ToString(); string str = String.Format("{0:#,###,###.##}",ips);Fultz
If your string isn't already formatted, it's impossible to divide it. How could I know if 11111 is 1.1.1.11 or 1.1.11.1? And what was dr["block_ip"] before you ToString() it?Surfperch

© 2022 - 2024 — McMap. All rights reserved.