Created
March 1, 2019 16:08
-
-
Save z5ottu/c3bc590617a667ff96eecef5aba32e42 to your computer and use it in GitHub Desktop.
Erlang cowboy IPv6 address string conversion to readable string in Elixir.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Utils.Cowboy do | |
use Bitwise | |
# | |
# ipv4 cowboy string example: "0.0.0.0.0.65535.20060.31747" | |
# ipv6 cowboy string example: "10754.43916.45826.36224.35230.44219.15941.63302" | |
def erlangCowboyIpv6StringToString(string) do | |
if String.starts_with?(string, "0.0.0.0.0.65535") do | |
String.split(string,".") |> Enum.take(-2) |> Enum.map(fn(x)-> String.to_integer x end) |> Enum.map(fn(x)-> [x >>> 8,x &&& 255] end) |> List.flatten |> Enum.join(".") | |
else | |
String.split(string,".") |> Enum.map(fn(x)-> String.to_integer x end) |> Enum.map(fn(x)-> Integer.to_string(x,16) end) |> Enum.join(".") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment