jeramyRR
How to pad a binary with 0's?
Hi all,
I’m trying to write out a string, in binary, using Elixir’s <<>> syntax, and am having a dumb moment. I have a requirement where if a template name string isn’t 16 characters long then the remaining bytes must be padded with zeros.
Here is the header I’m trying to write:
┌──────────────────┬───────────┬────────────────┬──────────────────┬───────────┬───────────┐
│ TAG │ Label Len │ Content Length │ Template Name │ Contents │ Signature │
├──────────────────┼───────────┼────────────────┼──────────────────┼───────────┼───────────┤
│4x Unsigned Chars │ u16 │ u64 │ 16byte char str │ │ │
└──────────────────┴───────────┴────────────────┴──────────────────┴───────────┴───────────┘
What I have so far is:
label_bin =
<<"TAG1", label_metadata.label_len::16, label_metadata.content_length::64>>
I’m not sure how to write the Template name in the same way as above. Do I need to create another IO structure and just append the 0’s?
Thanks,
Jeramy
Marked As Solved
Sebb
template_name = "foo"
pad = 16 - byte_size(template_name)
<<template_name::binary, 0::pad*8>>
Edit: Note that this may crash: How to pad a binary with 0's? - #6 by al2o3cr
Also Liked
Sebb
here: Kernel.SpecialForms — Elixir v1.12.3
also useful: binary — OTP 29.0.2 (stdlib 8.0.1)
(but there is nothing there that helps you with this, would be nice).
Don’t use String for this.
al2o3cr
The given solution will crash if template_name is longer than 16 characters; this may be fine, depending on where the name is coming from etc.
If crashing isn’t acceptable, than the string will need to be truncated. You can do both the padding and the truncating together with a little pattern matching:
<<padded_and_trimmed::bits-size(128), _::bits>> = <<template_name::binary, 0::128>>
This works by initially padding template_name with 16 bytes of zeros, then taking only the first 16 bytes of the remainder.
The truncation can also be done as part of a larger step, like your label_bin:
padded_name = <<template_name::binary, 0::128>>
label_bin =
<<"TAG1", label_metadata.label_len::16, label_metadata.content_length::64, padded_name::bits-size(128), ...>>
Last Post!
al2o3cr
The given solution will crash if template_name is longer than 16 characters; this may be fine, depending on where the name is coming from etc.
If crashing isn’t acceptable, than the string will need to be truncated. You can do both the padding and the truncating together with a little pattern matching:
<<padded_and_trimmed::bits-size(128), _::bits>> = <<template_name::binary, 0::128>>
This works by initially padding template_name with 16 bytes of zeros, then taking only the first 16 bytes of the remainder.
The truncation can also be done as part of a larger step, like your label_bin:
padded_name = <<template_name::binary, 0::128>>
label_bin =
<<"TAG1", label_metadata.label_len::16, label_metadata.content_length::64, padded_name::bits-size(128), ...>>
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









