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.16.2

Where Next?

Popular in Questions Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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