KalleJoP

KalleJoP

Hey folks,

I have an Elixir application that communicates with a Java application through ports.
For communication, I convert values into binary format using :erlang.term_to_binary/1.

However, I’ve noticed that when :erlang.term_to_binary/1 receives a list of integers, it returns an incorrect binary.

For example:
Input: [2]
Expected: <<131, 108, 0, 0, 0, 1, 97, 2, 106>>
Result: <<131, 107, 0, 1, 2>>

Can anyone help me avoid this issue?

Thank you :smiley:

Best regards,
Kalle

Showing Posts 1 to 10

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey @KalleJoP I also get the same result, but Erlang at least seems to have no problem converting it back to a list:

iex(1)> [2] |> :erlang.term_to_binary |> :erlang.binary_to_term
[2]

Can you elaborate why you expect <<131, 108, 0, 0, 0, 1, 97, 2, 106>>?

patrickdm

patrickdm

out of curiosity I just

iex(1)> :erlang.binary_to_term <<131, 108, 0, 0, 0, 1, 97, 2, 106>>                                                
[2]
iex(2)> :erlang.binary_to_term <<131, 107, 0, 1, 2>>                                                               
[2]
iex(3) (:erlang.binary_to_term <<131, 108, 0, 0, 0, 1, 97, 2, 106>>) == (:erlang.binary_to_term <<131, 107, 0, 1, 2>>)
true

:thinking:

paulanthonywilson

paulanthonywilson

That’s the missing parenthesis . Without the parenthesis round your function calls that I think line is being interpreted as

: erlang.binary_to_term(<<131, 108, 0, 0, 0, 1, 97, 2, 106>>  == :erlang.binary_to_term<<131, 107, 0, 1, 2>>)   

which is the same as

erlang.binary_to_term(false)
iex(1)> :erlang.binary_to_term(<<131, 108, 0, 0, 0, 1, 97, 2, 106>>) == :erlang.binary_to_term(<<131, 107, 0, 1, 2>>)  
true

I don’t know off the top of my head why the the two binary forms are equivalent. I could guess but I am trying to avoid being further nerd sniped by this question right now. :-/

LostKobrakai

LostKobrakai

Seems like there’s a list and string type:

Sounds like there’s overlap for charlists.

patrickdm

patrickdm

yes, I just found I was missing the parenthesis! Puzzled and intrigued too… :slight_smile:

hst337

hst337

There are no parenthesis missing

patrickdm

patrickdm

I edited my previous post without making it evident :frowning: my bad

patrickdm

patrickdm

Thank you.. I don’t know why I’m always so scared to go an look into Erlang docs .. :grimacing: :japanese_ogre:

paulanthonywilson

paulanthonywilson

Heh

iex(1)> :erlang.term_to_binary('hello') 
<<131, 107, 0, 5, 104, 101, 108, 108, 111>>
iex(2)> :erlang.term_to_binary('🧐')   
<<131, 108, 0, 0, 0, 1, 98, 0, 1, 249, 208, 106>>
iex(3)> '🧐'
[129488]
iex(4)> :erlang.term_to_binary([129488])
<<131, 108, 0, 0, 0, 1, 98, 0, 1, 249, 208, 106>>

[Edit] - For a minute I’d fooled myself into thinking that Erlang was intentionally supporting unicode in charlists but I wasn’t thinking straight. It’s just an artefact of Elixir’s charlist support

iex(1)> cs = 'hello 🧐'
[104, 101, 108, 108, 111, 32, 129488]
iex(2)> :erlang.iolist_to_binary(cs)
** (ArgumentError) errors were found at the given arguments:

  * 1st argument: not an iodata term

    :erlang.iolist_to_binary([104, 101, 108, 108, 111, 32, 129488])
    iex:2: (file)

``

Also 

> Only if a string contains code points < 256, can it be directly converted to a binary 

https://www.erlang.org/doc/apps/stdlib/unicode_usage.html#standard-unicode-representation

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
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
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
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
Stef-Verniers
Hello I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New

Other Trending Topics Top

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
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
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
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews