elbasti

elbasti

Why does String.to_integer/1 give strange results with with some values?

Hi folks, I’ve been running into some very strange behavior that has me knocking my head against the wall.

If I run the following code:

["42", "12", "15"]
|> Enum.map(&(String.to_integer(&1)))

I get what you’d expect: the list [42, 12, 15]

But if I run this code:

["41", "92", "73", "84", "69"] 
|> Enum.map(&(String.to_integer(&1)))

I get this weird string: ~c")\\ITE"

In fact, if I run just this:

["41"] 
|> Enum.map(&(String.to_integer(&1)))

I get ~c")"

What on earth is going on?!

I figured “maybe there’s some unicode strageness going on with LiveBook” so I tried it in iex, with the same results:

Most Liked Switch mode

outlog

outlog

congrats on reaching this milestone - and completing a major step/rite of passage on your elixir journey - it’s a charlist Binaries, strings, and charlists — Elixir v1.21.0-dev

we have all been there - and gone through the same wtf etc.

17
Post #1
dimitarvp

dimitarvp

Yeah, you are basically seeing the runtime trying to be a bit too helpful – if a list of integers looks like a list of printable characters, then the REPL will instead show them as a printable string. The data is still there and is still the same.

If you want to get rid of that behavior in your project, just create an .iex.exs file at the root of your project and put this in there:

IEx.configure(inspect: [charlists: :as_lists])

Restart your iex session and, et voila:

iex(1)> ["41"] |> Enum.map(&String.to_integer/1)
[41]
LostKobrakai

LostKobrakai

I think that would just surface how often we actually run into charlists when interacting with erlang libraries. E.g. hardly any elixir app doesn‘t use gen_tcp somewhere.

From my personal experience I‘ve been hitting the case of needing to see the underlying list of integers mostly in tests or puzzles, where there is time and opportunity to deal with the printed formatting. Not optimal, but fine.

Interacting with actual charlists on the other hand comes up when you‘re debugging some production issue and trying to dig deeper into some failure, where you don‘t want to deal with first setting up useful formatting rules to be able to understand what the system is trying to provide in information.

Last Post!

7stud

7stud

Check this out:

Interactive Elixir (1.14.4) - press Ctrl+C to exit (type h() ENTER for help)

iex(1)> [97,98,99]
'abc'

iex(2)> [97,98,99,0]
[97, 98, 99, 0]

iex(3)> 'abc' === [97,98,99]
true

iex(4)> Enum.map('abc', fn n -> n*2 end)
[194, 196, 198]

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement