pedromvieira
Parse IPV4 or IPV6 bitstrings
Hi, I’am parsing OTPCertificates obtain with :ssl.
Some alternative names has IPV4 or IPV6 bitstrings like:
[
iPAddress: <<1, 1, 1, 1>>,
iPAddress: <<1, 0, 0, 1>>,
iPAddress: <<162, 159, 132, 53>>,
iPAddress: <<38, 6, 71, 0, 71, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 17>>,
iPAddress: <<38, 6, 71, 0, 71, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 1>>,
iPAddress: <<38, 6, 71, 0, 71, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100>>,
iPAddress: <<38, 6, 71, 0, 71, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 0>>,
iPAddress: <<162, 159, 36, 1>>,
iPAddress: <<162, 159, 46, 1>>
]
I successfully translated IPV4 ones (Ex: “162.159.132.53”) with this code:
<<162, 159, 132, 53>>
|> :unicode.characters_to_binary(:latin1, :utf8)
|> to_charlist()
|> List.to_tuple()
|> :inet_parse.ntoa()
|> to_string()
But I couldn’t translate IPV6. Any ideas?
<<38, 6, 71, 0, 71, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 17>> should translate to “2606:4700:4700:0:0:0:0:1111”
Marked As Solved
voltone
You may want to use binary comprehensions, for both v4 and v6:
for <<field <- ipv4>> do field end
|> List.to_tuple()
|> :inet.ntoa()
|> to_string()
for <<group::16 <- ipv6>> do group end
|> List.to_tuple()
|> :inet.ntoa()
|> to_string()
Note that inet_parse is an internal, undocumented module: use the public inet:ntoa/1 instead.
5
Popular in Questions
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
Hi all,
I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I'm trying to use Postg...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> someth...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
In AR this is so simple
@articles = current_user.articles
How to do in Ecto?
def index(conn, _params) do
current_user = conn.assig...
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Other popular topics
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New







