I have the weirdest behavior in my perl I just don't seem to figure out. I am parsing a text file, creating a binary sting along the way:
$TDI = $a_stat . "$TDI"; #$a_stat = 0/1
if ( $b_stat ne "X" ) { $TDO = $b_stat . "$TDO" } #$b_stat is 0/1
now, i want to print it formatted, according to the size:
$inputSize = length($TDI);
$hexSize = sprintf("%.0f", ($inputSize/4)+0.4); # rounding up
printf ("$inputSize TDI (%0${hexSize}X) TDO (%0${hexSize}X)\n", bin2dec $TDI, bin2dec $TDO);
This works out fine for 32 bit, but when I change the input size (specifically to input 26, hex 7) it prints out zeros before the hex value. I added a little debug line printf "$inputSize, $hexSize, (%0${hexSize}X)\n", oct($TDI)
and got the following (quite amazing) results:
...
24, 6, (0000AA)
25, 7, (00000000000000000000000000AA) <- 22 unneeded zeros
26, 7, (00000000000000000000000000AA) <- 22 unneeded zeros
27, 7, (00000000000000000000000000AA) <- 22 unneeded zeros
28, 7, (00000000000000000000000000AA) <- 22 unneeded zeros
29, 8, (000000AA)
...
(now handling another string)
25, 7, (0000000000000000000000000000000000000000000000000000000000000008) <- 56 unneeded zeros
26, 7, (0000000000000000000000000000000000000000000000000000000000000008) <- 56 unneeded zeros
27, 7, (0000000000000000000000000000000000000000000000000000000000000008) <- 56 unneeded zeros
28, 7, (0000000000000000000000000000000000000000000000000000000000000008) <- 56 unneeded zeros
29, 8, (00000008)
...
At the next loop it gets to 121 zeros and also add 30 zeros to the 5-8 range.
I checked the bin2dec results and they are fine.
Can anyone figure out why I get this anomaly? If possible, please try to refrain from CPAN based suggestions as I'm using perl on a machine disconnected from the net with limited access to CPANs (at work).
EDIT
if someone can suggest a better way of finding the size of the hexa number representing my TDO\ TDI binary strings and\ or can come up with a better idea of how to have agile zeros padding than %0{$hexSize}
I'll be happy to hear, specially since it might nullify the problem. Even though I like my code, Its not a catholic marriage...
Thanks for all you helpers and editors.