lud

lud

Binary to integer (no parsing)

Hi,

Is there a function that would convert <<1>> to 1, <<1, 0>> to 256. I do not mean parsing the binary as a string representation of the integer.

I want to know how bing a 20 bytes integer can be.

b = 1..20 |> Enum.map(fn _ -> 255 end) |> :erlang.list_to_binary
<<255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255, 255, 255, 255, 255>>

Thank you.

Marked As Solved

voltone

voltone

Try :binary.decode_unsigned/1,2, it does exactly what your code does…

Also Liked

LostKobrakai

LostKobrakai

iex(2)> <<int::integer-size(2)-unit(8)>> = <<255, 255>>
<<255, 255>>
iex(3)> int
65535
NobbZ

NobbZ

There are 2^n possible values in n bits.

ityonemo

ityonemo

If you’re just doing one number, you can use pattern matching likr this:

<<number::16>> = <<1, 0>>

And that will give you 256. This strategy will work for a 20 byte integer (obviously not with 16). Though if you’re using it as a literal I recommend number=0xFFFFFFFF....FFFFF

dimitarvp

dimitarvp

Your local thread necromancer checking in.

I am gonna work in my free time on a fairly basic network protocol in Rust, Golang and Elixir, and I am starting with Elixir.

I need to be able to encode and decode unsigned 32-bit integers to/from byte buffers (big endian). While :binary.encode_unsigned(x, :big) works the protocol requires all 4 bytes be sent, and this function always finds the smallest possible representation:

iex(1)> :binary.encode_unsigned 0xFF, :big
<<255>>

Whereas I need <<0, 0, 0, 255>>.

So just to bring visibility in case someone ever needs, encoding is as simple as:

iex(1)> <<255::unsigned-big-integer-32>>
<<0, 0, 0, 255>>

And decoding is done like so:

iex(1)> :binary.decode_unsigned(<<0, 0, 0, 255>>, :big)
255

(Taken from the docs of <<>> and :binary.)

LostKobrakai

LostKobrakai

I’d suggest the inverse from encoding, to keep things mirrored/simple:

iex(1)> <<int::unsigned-big-integer-32>> = <<0, 0, 0, 255>>
<<0, 0, 0, 255>>
iex(2)> int
255

Where Next?

Popular in Questions 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
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
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

Other popular topics Top

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
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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
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

We're in Beta

About us Mission Statement