I'm trying to convert one IP from string to Decimal using split
and aggregate
. Specifically I made this question about Split and Aggregate not other solutions like IpAddress or similar.
For the "192.168.0.1"
ip, the number must be 3232235521
var decimalIP = "192.168.0.1".Split(".").Aggregate(0, (numIP, segment) => {
numIP = numIP<< 8;
return numIP + Decimal.Parse(segment);
});
This method does not work:
Decimal number = "192.168.0.1".Aggregate(0m, (numIP, segment) => {
numIP = (long)numIP << 8;
return (Decimal)(numIP + Decimal.Parse(segment));
});
I was also trying
var segments = "192.168.0.1".Select(segment => Decimal.Parse(segment));
var reverse = segments.Reverse();
Decimal reverseSumm = reverse.Aggregate(0m, (numIP, segment) => {
numIP = (long)numIP << 8;
return (Decimal)(numIP + segment);
});
Decimal directSumm = segments.Aggregate(0m, (numIP, segment) => {
numIP = (long)numIP << 8;
return (Decimal)(numIP + segment);
});
The number
How to do it?