The following function converts a given IPv4 IP address into an eight char hex string. Two hex chars in a row presenting eight bits of the IP address.
The idea behind it was to write a nice PowerShell function and to make the IP addresses easily sortable in a database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | function ipv4ToHexString($ipv4){ # Validation $valid = $true $ipv4.split(".") | foreach { if([int]$_ -ge 0 -and [int]$_ -le 255) { } else { $valid = $false } } if($valid -eq $true){ # Conversion $hexString = "" $hexList = @("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f") $ipv4.split(".") | foreach { $mod = [int]$_ % 16 $first = $hexList[(([int]$_ - $mod) / 16)] $second = $hexList[$mod] $hexString += $first+$second } return $hexString } else{ return $false } } |