The Perl subroutine seems rather complicated for what it does, viz., taking chunks of two chars at a time (the first 16 chars) from the sent string and then reverses it. Another Perl option is:
sub reverse_hex {
return join '', reverse unpack 'A2' x 8, $_[0];
}
First, unpack
here takes two characters at a time (eight times) and produces a list. That list is reverse
d and join
ed to produce the final string.
Here's a Python subroutine to accomplish this:
def reverse_hex(HEXDATE):
hexVals = [HEXDATE[i:i + 2] for i in xrange(0, 16, 2)]
reversedHexVals = hexVals[::-1]
return ''.join(reversedHexVals)
The list comprehension produces eight elements of two characters each. [::-1]
reverses the list's elements and the result is join
ed and returned.
Hope this helps!