Use pack with the Z template.
Update: An example.
johngg@aleatico:~$ perl -Mstrict -Mwarnings -E '
my $str = q{example.me};
my $length = length $str;
my $padTo = 4;
my $rem = $length % $padTo;
my $padLen = $length + ( $rem ? $padTo - $rem : 0 );
print pack qq{Z$padLen}, $str;' | hexdump -C
00000000 65 78 61 6d 70 6c 65 2e 6d 65 00 00 |example.m
+e..|
0000000c
Update 2: Don't use this, if the string is already aligned to a 32-bit boundary the Z template will overwrite the last character with a NULL is it seems to always produce a C-style NULL-terminated string.
Update 3: Removed the strike-through! The solution is to only pad with pack when necessary but it does begin to look messy.
johngg@aleatico:~$ perl -Mstrict -Mwarnings -E '
my $str = q{myexample.me};
my $length = length $str;
my $padTo = 4;
my $rem = $length % $padTo;
print $rem
? pack( qq{Z@{ [ $length + $padTo - $rem ] }}, $str )
: $str;' | hexdump -C
00000000 6d 79 65 78 61 6d 70 6c 65 2e 6d 65 |myexample
+.me|
0000000c
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|