The first step is to understand what the code is doing:
The code seems to convert an array of bytes to a string of hex characters where the lowest byte is on the right side of the string (it depends on the definition of the
HEX
array but I guess it is just a mapping to the characters 0 to 9 and a to f).
You can then search the web for something like "perl hex array to string". Doing so, you will find solutions like
perl - Convert an array of integer into a string of HEX - Stack Overflow[
^].
From my point of view you should use one of the powerful Perl conversion functions:
unpack - perldoc.perl.org[
^]. If the input argument is an array, see the above SO link.
If it is any variable containing binary data, it is much simpler:
my( $hexString ) = unpack( 'H*', $bytes );
Example:
my $bytes = "123\x0A";
my( $hexString ) = unpack( 'H*', $bytes );
print "$hexString\n";
Output:
3132330a