Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Align string on a 32-bit boundary with padding

by BillKSmith (Monsignor)
on Aug 20, 2022 at 20:50 UTC ( [id://11146272]=note: print w/replies, xml ) Need Help??


in reply to Re: Align string on a 32-bit boundary with padding
in thread Align string on a 32-bit boundary with padding

After studying your updates, private messages, and documentation for 'pack', I was able to fix my previous code by packing one extra (arbitrary) character, and not unpacking it. However, I noticed that the 'pack' was overkill. I could concatenate three extra nulls to the end of the original ASCII string and unpack as much as necessary. The length ($l) has to be twice what it was before because now I am unpacking two hex characters for every one ASCII character that I had been packing.
use strict; use warnings; use Test::More tests=>4; my @examples = ( [qw( example. 6578616d706c652e )], [qw( example.1 6578616d706c652e31000000)], [qw( example.12 6578616d706c652e31320000)], [qw( example.123 6578616d706c652e31323300)], ); foreach my $example(@examples) { my ($Origin_Host_CER, $required) = @$example; my $l = 8*int((length($Origin_Host_CER)+3)/4); my $result = unpack("H$l", $Origin_Host_CER . "\x00" x 3); is($result, $required, $Origin_Host_CER); }

RESULTS

1..4 ok 1 - example. ok 2 - example.1 ok 3 - example.12 ok 4 - example.123
Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11146272]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-16 06:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found