#! /usr/bin/env perl use strict; use warnings; use 5.010.000; #> sub routines #> -------------------------------------------------------------------------- sub hexstring_bitwise_reverted { # see output of "perldoc -f convert"; or see manually in perlfaq4 return sprintf "0x%02X", oct( "0b" . reverse sprintf( "%08b", oct(shift) ) ); # | | | | | | # | | | | | fetch first argument (a hex string 0xAB) # | | | | convert it to decimal # | | | create bitstring of decimal # | | invert the order of the bits in bitstring # | convert reverted bitstring back to decimal # create a hexstring again to return } #> main program #> -------------------------------------------------------------------------- my $hexstring = "0xFC"; say join "\n", $hexstring, hexstring_bitwise_reverted( $hexstring );