jeramyRR

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

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

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

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

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), ...>>

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42533 114
New

We're in Beta

About us Mission Statement