Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save z5ottu/c3bc590617a667ff96eecef5aba32e42 to your computer and use it in GitHub Desktop.
Save z5ottu/c3bc590617a667ff96eecef5aba32e42 to your computer and use it in GitHub Desktop.
Erlang cowboy IPv6 address string conversion to readable string in Elixir.
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