in reply to Port Java code to Perl help

Assuming you get a maximum of four hex digits in a number between dashes (otherwise the semantics of the Java version would be rather obscure too), I'd say:

sub get_bytes { return join '', map { length($_) > 2 ? pack('C', hex substr($_,0,-2)) : pack('C', he +x $_) } split /-/, $_[0]; }

It splits the string on dashes, the resulting list gets mapped to characters depending on the length of the number (I leave off the last two hex digits rather than doing a logical right shift because pack returns a string) and then joined back together with no separator to yield the byte string.

Maybe someone can come up with a clever pack pattern that does it all, I'm not quite the expert there.

Replies are listed 'Best First'.
Re^2: Port Java code to Perl help
by Eliya (Vicar) on Apr 14, 2012 at 09:34 UTC

    As pack 'H' is working from left to right on the passed string, you can simply say pack 'H2' (two hex characters make up one byte):

    sub get_bytes { return join '', map { pack('H2', $_) } split /-/, $_[0]; } print get_bytes('66-6f-6f00-62ff-61-72');

    This would print "foobar", because the lower bytes 00 and ff are being ignored.