neenjaw

neenjaw

How to best create bitstrings / binary data with known values with mixed types

Hi,

I have a general question about building bitstring/binary data. I have been following some articles that look at parsing binary data, so I feel like I have some handle on matching a binary, and bringing parts to variables, but I am having some difficulty creating binary data from known data with non-binary types.

# this makes sense to me
iex> <<first::binary-size(1), rest::binary>> = "hello world"
"hello world"
iex> first                                                  
"h"

# This also makes sense
iex> first                                               
"h"
iex> rest                                                
"ello world"
iex> <<first::binary, rest::binary>>                   
"hello world"

# But how now do I encode the integer 1 as a 4-or-8-byte block into the bitstring
iex> n = 1024                          
1024
iex> <<first::binary, _______, rest::binary>>

What I would hope to find out, is there a way for me to perform this in that form, or is the only way to do it is to go about it the long way:

iex(48)> first                                               
"h"
iex(49)> rest                                                
"ello world"
iex> n = 1024
1024
iex> n_as_bin = 
...>   n 
...>   |> :binary.encode_unsigned(:big) 
...>   |> :binary.bin_to_list() 
...>   |> (fn b -> List.duplicate(0, 4-length(b)) ++ b end).() 
...>   |> :binary.list_to_bin
<<0, 0, 4, 0>>
iex> <<first::binary, n_as_bin::binary, rest::binary>>                                                                                                    
<<104, 0, 0, 4, 0, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100>>

Thanks much in advance.

Marked As Solved

ityonemo

ityonemo

I think you want this:

iex(1)> first = "h"
"h"
iex(2)> n = 1024
1024
iex(3)> rest = "ello"
"ello"

# specify the number of bits as your binary segment decorator
iex(4)> <<first::binary, n::32, rest::binary>>
<<104, 0, 0, 4, 0, 101, 108, 108, 111>>
iex(5)> <<first::binary, n::64, rest::binary>>
<<104, 0, 0, 0, 0, 0, 0, 4, 0, 101, 108, 108, 111>>

# note that you can pattern match out of it too.
iex(6)> <<_first::binary-size(1), res::64, _rest::binary>> = v
<<104, 0, 0, 0, 0, 0, 0, 4, 0, 101, 108, 108, 111>>
iex(7)> res
1024

You can also do ‘crazy things with bitbashing’, those numbers don’t have to be multiples of 8.

iex(9)> <<1::1, 2::4, 1::3>> 
<<145>>

Also Liked

neenjaw

neenjaw

That is exactly what I am looking for! I just couldn’t see the tree through the forest, thanks so much!

Where Next?

Popular in Questions Top

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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
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
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
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
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
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
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement