in reply to Program to convert Numeric text file to Packed decimal file
Assuming that by "packed decimal" you mean COBOL style COMP3, try this (unverified):
sub iToComp3 { my $num = '' . shift; ## force to string ## Add padding to even length strings ## to accomodate trailing sign bits $num = '0' . $num unless length( $num ) & 1; my $comp3 = ''; ## pack each digit into 4 bits of output vec( $comp3, $_, 4 ) = substr( $num, $_, 1 ) for 0 .. length( $num ) - 1; ## Add sign bits (doesn't cater for unsigned COMP3!) vec( $comp3, length( $num ), 4 ) = $num < 0 ? 0x0D : 0x0C; return $comp3; }
|
|---|