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!
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
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
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
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
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
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
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
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
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
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
benwilson512
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
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 tryh Kernel.<tab>.For example, for the last step in your problem, I’d
h String.<tab>, and fortunately, I can findpad_leading/2andpad_leading/3in the function list, then Ih String.pad_leadingto view their documentation.hauleth
Just format it properly -
List.to_string(:io_lib.format("~8.2B", [3]))ericgray
You can break this down into 3 easy steps using the modules that @benwilson512 recommends.
Convert string to integer
String.to_integer("3")Get binary value of integer
Integer.to_string(3, 2)Pad binary string with zeros
String.pad_leading("11", 8, "0")You can write the code with guard clauses to handle strings or integers.
hauleth
Still IMHO overcomplicated:
wojtekmach
This gives leading whitespaces, not zeroes. Do you know if there’s a modifier to make it do zeroes?
hauleth
Oh yeah, it should be
"~8.2.0B"Papillon6814
Thank you!
All suggestions helped me!
slouchpie
None of the “written code” solutions here work for numbers greater than 255.
I think this solution works for all (positive) numbers:
It could probably be neater but at least it works for numbers larger than 255.
Any improvements are very welcome!