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 |