What is the fastest way of converting the dotted format of the following IP from version 6 to colon format??
128.91.45.157.220.40.101.10.10.1.252.87.22.200.31.255
I just typed the IP above randomly.
Thanks
var result = new IPAddress(x.Split('.').Select(byte.Parse).ToArray()).ToString();
// result == "805b:2d9d:dc28:650a:a01:fc57:16c8:1fff"
The fastest way would be to do all parsing and conversion yourself.
This is more than ten times faster than the currently accepted answer using Split, Select and IPAddress:
string ip = "128.91.45.157.220.40.101.10.10.1.252.87.22.200.31.255";
StringBuilder b = new StringBuilder(8 * 4 + 7);
string hex = "0123456789abcdef";
int pos = 0;
for (int i = 0; i < 16; i++) {
int n = 0;
while (pos < ip.Length && ip[pos] != '.') {
n = n * 10 + (ip[pos++] - '0');
}
pos++;
b.Append(hex[n / 16]);
b.Append(hex[n % 16]);
if (i % 2 == 1 && i < 15) {
b.Append(':');
}
}
return b.ToString();
Note: This code does not omit leading zeroes, it always produces a string with eight four-digit values.
This is the times per operation that I get from running each a million times:
Fast: 0,00038 ms.
Linq: 0,00689 ms.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With