Papillon6814

Papillon6814

How to convert decimal value to binary value?

I want to convert “3” to “11”, and then “11” to “00000011” in elixir.
But I don’t know any ways to do it.

Help me!

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

When I don’t know how to do something in Elixir, the first place I look is the module that has code related to the kind of data I’m trying to deal with. In this case, you’re trying to turn a String into an integer, and then represent that integer in a particular base.

Turning a string into an integer can be accomplished with a function found in this module: https://hexdocs.pm/elixir/String.html

And turning an integer into the digits of a specific base can be found with a function in this module: https://hexdocs.pm/elixir/Integer.html

Give it a shot! We’ll be here if you have further questions.

Also Liked

ericgray

ericgray

You can break this down into 3 easy steps using the modules that @benwilson512 recommends.

  1. Convert string to integer String.to_integer("3")

  2. Get binary value of integer Integer.to_string(3, 2)

  3. Pad binary string with zeros String.pad_leading("11", 8, "0")

You can write the code with guard clauses to handle strings or integers.

  def decimal_to_binary(decimal) when is_binary(decimal) do
    decimal
    |> String.to_integer()
    |> decimal_to_binary()
  end

  def decimal_to_binary(decimal) when is_integer(decimal) do
    decimal
    |> Integer.to_string(2)
    |> String.pad_leading(8, "0")
  end
hauleth

hauleth

Oh yeah, it should be "~8.2.0B"

hauleth

hauleth

Just format it properly - List.to_string(:io_lib.format("~8.2B", [3]))

wojtekmach

wojtekmach

Hex Core Team

This gives leading whitespaces, not zeroes. Do you know if there’s a modifier to make it do zeroes?

Aetherus

Aetherus

As an alternative to @benwilson512 's method, sometimes I just fire up an iex and h SomeModule.<tab> or something alike, and search for the function that could work. If none is found, I’ll try h Kernel.<tab>.

For example, for the last step in your problem, I’d h String.<tab>, and fortunately, I can find pad_leading/2 and pad_leading/3 in the function list, then I h String.pad_leading to view their documentation.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
wernerlaude
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
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
shahryarjb
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
johnnyicon
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
Fl4m3Ph03n1x
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement