... add ("\0" x $n) to the original string.

Shouldn't that be ("\0" x ($n-1)) if $n is the boundary width in bytes?

Win8 Strawberry 5.8.9.5 (32) Sat 08/20/2022 15:46:20 C:\@Work\Perl\monks >perl use strict; use warnings; use bytes; use Data::Dump qw(pp); use constant BYTE_BOUND => 4; use constant PAD => "\0" x (BYTE_BOUND-1); my $s = 'TheRainInSpainFalls'; while (length $s) { my $p = $s . PAD; $p = bytes::substr $p, 0, bytes::length($p) - bytes::length($p)%BY +TE_BOUND; printf "%4s - %-28s \n", bytes::length($p), pp($p); chop $s; } ^Z 20 - "TheRainInSpainFalls\0" 20 - "TheRainInSpainFall\0\0" 20 - "TheRainInSpainFal\0\0\0" 16 - "TheRainInSpainFa" 16 - "TheRainInSpainF\0" 16 - "TheRainInSpain\0\0" 16 - "TheRainInSpai\0\0\0" 12 - "TheRainInSpa" 12 - "TheRainInSp\0" 12 - "TheRainInS\0\0" 12 - "TheRainIn\0\0\0" 8 - "TheRainI" 8 - "TheRain\0" 8 - "TheRai\0\0" 8 - "TheRa\0\0\0" 4 - "TheR" 4 - "The\0" 4 - "Th\0\0" 4 - "T\0\0\0"


Give a man a fish:  <%-{-{-{-<


In reply to Re^3: Align string on a 32-bit boundary with padding by AnomalousMonk
in thread Align string on a 32-bit boundary with padding by Lucas Rey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.