Pad string with characters

70 Views Asked by At

I have

$data_dec = 7;
$data_bin = sprintf("%08b",data_dec);

and $data_bin is

00000111

How do I pad with "X" instead of zeros while maintaining 8-bits? Expected data:

XXXXX111
1

There are 1 best solutions below

0
ikegami On
substr( sprintf( "XXXXXXX%b", $n ), -8 )
sprintf( "%8b", $n ) =~ tr/ /X/r