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>>






















