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
Tee
can someone please explain to me how Enum.reduce works with maps
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
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
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement