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: String — Elixir v1.20.2

And turning an integer into the digits of a specific base can be found with a function in this module: Integer — Elixir v1.20.2

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]))

Last Post!

slouchpie

slouchpie

None of the “written code” solutions here work for numbers greater than 255.

I think this solution works for all (positive) numbers:

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

  def decimal_to_binary(num) when is_integer(num) do
    binary_string = :erlang.integer_to_binary(num, 2)
    binary_string_length = String.length(binary_string)
    desired_string_length = binary_string_length + (8 - rem(binary_string_length, 8))
    String.pad_leading(binary_string, desired_string_length, "0")
  end

It could probably be neater but at least it works for numbers larger than 255.

Any improvements are very welcome!

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

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
ashish173
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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42533 114
New
JorisKok
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
senggen
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

We're in Beta

About us Mission Statement