Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get the length of a binary in Elixir?

Tags:

elixir

How to you get the length of a binary in bytes? It's not a string. I don't want the number of characters. I just want to know how many bytes long it is.

like image 385
Matt Avatar asked Jan 28 '23 23:01

Matt


1 Answers

You could leverage byte_size for that purpose as documented here

Returns the number of bytes needed to contain bitstring.

That is, if the number of bits in bitstring is not divisible by 8, the resulting number of bytes will be rounded up (by excess). This operation happens in constant time.

Allowed in guard tests. Inlined by the compiler.

Examples

iex> byte_size(<<433::16, 3::3>>) 3

iex> byte_size(<<1, 2, 3>>)

like image 174
Kevin Johnson Avatar answered Mar 19 '23 06:03

Kevin Johnson