henry-iteron

henry-iteron

Elixir converting [55] into '7'

In Elixir, I have to use the list of integers and work with that,

If my input is [55] it gives the data as ‘7’ but I need it to be [55] only. How can I maintain it?

If I try this as [‘55’], it’s working fine. But I want to work with [55] which is a list of integers.

Is there any solution available?

Marked As Solved

sbuttgereit

sbuttgereit

The other two replies are exactly correct and I’m not going to add any new facts. All I’m going to try to do is simplify those comments a bit.

This is the root of the problem: a charlist can be thought of as special sort of string and it’s just a list of integers… which is indistinguishable from any other list of integers where you really want just the numbers. Because Elixir can’t always tell the difference between a list of integers which is just a list of integers and a list of integers which is meant to be a charlist is makes a default choice when it comes to displaying the list: treat the integers as ASCII values and print those characters. In your case it’s wrong, but as the others said, its just a display issue: your list of integers is there as you intended and will work the way you intend outside of this output behavior. Importantly, note that the actual list and its values are not transformed from what you expect: this is just the way your values are interpreted for display.

This is the way to change the default output of that list. In your case, (assuming you’re using IO.inspect/2), the default assumption that a list of valid ASCII codes is ASCII is wrong and the option described above changes that default assumption for that run of the function. This doesn’t change the actual list values, that’s always the [55] you intended, just the way they are displayed.

Also Liked

sodapopcan

sodapopcan

Welcome to the community! You have completed your first rite of passage by asking this question :smiley: It’s all downhill from here!

tj0

tj0

It’s not converting, it’s just a display issue.

A charlist is a list of integers. 55 is the decimal code for ascii “7”.

iex> IO.inspect([55], [charlists: :as_lists])

To see all the options:

iex> h Inspect.Opts 
bartblast

bartblast

Creator of Hologram

Hey @henry-iteron, welcome to the community :slight_smile:
I assume you’re using IO.inspect/2? To get what you want you need the :charlists opt, e.g.

IO.inspect([55], charlists: :as_lists)

Take a look here: Inspect.Opts — Elixir v1.20.2

Where Next?

Popular in Questions Top

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
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

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 41539 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
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement