Papillon6814

Papillon6814

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!

Showing Posts 1 to 9

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.

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.

hauleth

hauleth

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

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

Still IMHO overcomplicated:

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

def decimal_to_binary(num) when is_integer(num),
  do: List.to_string(:io_lib.format("~8.2B", [num]))
wojtekmach

wojtekmach

Hex Core Team

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

hauleth

hauleth

Oh yeah, it should be "~8.2.0B"

Papillon6814

Papillon6814 OP

Thank you!
All suggestions helped me!

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!

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
csmr
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley: Mutonex gamedesign was finish...
New
perzanko
Do we have maintainer of absinthe-graphql/dataloader here? We have a PR that we’d like to merge to the library. Relatively small additive...
New

Other Trending Topics Top

rodloboz
Hi everyone! I’d like to share Fude (筆, “brush”), a charting library for Phoenix LiveView. I built it for dashboards and reports where y...
New
dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews