Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: unpacking mixed ascii & utf16 null termed strings

by ikegami (Pope)
on Oct 05, 2011 at 00:23 UTC ( #929691=note: print w/ replies, xml ) Need Help??


in reply to unpacking mixed ascii & utf16 null termed strings

The reverse of encode→pack is unpack→decode. You have to remove "layers" in the opposite order that they've been applied, so you're doing it in the wrong order.

I don't see any trivial way of extracting the string before it's decoded, unfortunately.

sub extract_text { $_[0] =~ s/^((?:..)*)\0\0//s or die; return decode('UTF-16le', "$1"); } my $first_name = extract_text($bytes); my $middle_name = extract_text($bytes); my $last_name = extract_text($bytes); my ($x, $y) = unpack('VV', substr($bytes, 0, 8, '')); my $address = extract_text($bytes); my $zip_code = extract_text($bytes); my ($z) = unpack('V', substr($bytes, 0, 4, ''));
or maybe
my @fields = $bytes =~ / ^ ((?:..)*)\0\0 ((?:..)*)\0\0 ((?:..)*)\0\0 (.{4}) (.{4}) ((?:..)*)\0\0 ((?:..)*)\0\0 (.{4}) \z /sx or die; $_ = decode('UTF-16le', $_) for @fields[0,1,2,5,6]; $_ = unpack('V', $_) for @fields[3,4,7];


Comment on Re: unpacking mixed ascii & utf16 null termed strings
Select or Download Code
Re^2: unpacking mixed ascii & utf16 null termed strings
by patcat88 (Deacon) on Oct 05, 2011 at 08:46 UTC
    Your regexp seems to work. It also deals correctly with the problem of matching 2nd byte (a \0) of hypothetical 1st char (a "p\0" or "\x70\0" lets say) and 1st byte of 2nd char (a \0) that is a utf 16 null (a \0\0), rather than byte 1 (\0) and byte 2 (\0) of utf16 null. I didnt think of doing your group and multiplier to do the alignment on the utf16 strings in a regexp. Thanks.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (13)
As of 2014-10-21 17:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (106 votes), past polls