in reply to How to use pack/unpack data over a network
And the testing section prints the expected: \x00\x00\x00\x0dHello, world!\x00\x00\x00 Since you're packing the unpadded length in the first four bytes, you'll have to make it the next multiple of 4 before unpacking the string as well.$str = 'Hello, world!'; $len = length $str; $packed_str = pack 'N', $len; $len += 4 - (($len % 4) || 4); # next multiple of 4 $packed_str .= pack "Z$len", $str; # for testing: ($test_str = $packed_str) =~ s/([^\x20-\x7E])/sprintf '\\x%02x', ord $ +1/ge; print "$test_str\n"; __END__
|
|---|