i have hex values in the format of 4a0e94ca etc, and i need to convert them into IP's, how can i do this in C# ?
Asked
Active
Viewed 5,275 times
3
-
1Dup - http://stackoverflow.com/questions/1139957/c-convert-int-to-hex-and-back-again – johnc Aug 30 '09 at 22:57
-
Sorry sniper, I see you're new here. Please search questions before posting a duplicate. The link to the answer you're looking for is above – johnc Aug 30 '09 at 22:59
-
1Whether that's a duplicate depends on how you define an IP, and what the output is (I assume it's to be converted to a string). – Matthew Iselin Aug 30 '09 at 22:59
-
Granted. I missed that, however I do feel the answer would be very similar. – johnc Aug 30 '09 at 23:02
-
1I agree, but at the same time the bitwise math componen that silky posted helps sniperX understand what's happening behind the scenes (which you don't get in that other question). That's just my opinion though! – Matthew Iselin Aug 30 '09 at 23:05
-
Got to agree I liked that answer – johnc Aug 30 '09 at 23:12
3 Answers
16
If the values represent IPv4 addresses you can use the long.Parse
method and pass the result to the IPAddress constructor:
var ip = new IPAddress(long.Parse("4a0e94ca", NumberStyles.AllowHexSpecifier));
If they represent IPv6 addresses you should convert the hex value to a byte array and then use this IPAddress constructor overload to construct the IPAddress.
-
-
-
1I think it should be noted, that this will only work when executed on a big endian system. – MrPaulch Nov 07 '14 at 12:43
3
Well, take the format of an IP in this form:
192.168.1.1
To get it into a single number, you take each part, OR it together, while shifting it to the left, 8 bits.
long l = 192 | (168 << 8) | (1 << 16) | (1 << 24);
Thus, you can reverse this process for your number.
Like so:
int b1 = (int) (l & 0xff);
int b2 = (int) ((l >> 8) & 0xff);
int b3 = (int) ((l >> 16) & 0xff);
int b4 = (int) ((l >> 24) & 0xff);
-- Edit
Other posters probably have 'cleaner' ways of doing it in C#, so probably use that in production code, but I do think the way I've posted is a nice way to learn the format of IPs.

Noon Silk
- 54,084
- 6
- 88
- 105
-
+1 - I believe knowing the bitwise math is probably the most important part of converting integers to IPs (and vice versa), even if your language does it for you. – Matthew Iselin Aug 30 '09 at 23:15
-
@Matthew: While I agree knowing Bitwise math is useful, most languages that are C derived have the function inet_ntop and inet_pton for exactly this purpose. – Powerlord Aug 31 '09 at 15:39
-
@R. Bemrose: Sure, but I think it's good to know how those functions *work*. Each to his own though, some people aren't as excited as I am about what's happening at the lower levels :) – Matthew Iselin Sep 01 '09 at 00:41
2
Check C# convert integer to hex and back again
var ip = String.Format("{0}.{1}.{2}.{3}",
int.Parse(hexValue.Substring(0, 2), System.Globalization.NumberStyles.HexNumber),
int.Parse(hexValue.Substring(2, 2), System.Globalization.NumberStyles.HexNumber),
int.Parse(hexValue.Substring(4, 2), System.Globalization.NumberStyles.HexNumber),
int.Parse(hexValue.Substring(6, 2), System.Globalization.NumberStyles.HexNumber));

Community
- 1
- 1

Yannick Motton
- 34,761
- 4
- 39
- 55
-
That is a pretty decent way, but maybe not the fastest. Still, +1. – John Gietzen Aug 30 '09 at 23:02
-
while this is a neat way to do it, it doesn't work for all cases i.e. 192.168.1.1. Since the solution will throw an error after chopping it into 19.21.68.11 – ji sung Choi Jul 29 '22 at 21:13