aloktiwary has asked for the wisdom of the Perl Monks concerning the following question:

Is there any way to convert numeric values txt file to PACKED DECIMAL File. Please help! Regards, Alok
  • Comment on Program to convert Numeric text file to Packed decimal file

Replies are listed 'Best First'.
Re: Program to convert Numeric text file to Packed decimal file
by Marshall (Canon) on May 22, 2009 at 08:21 UTC
    Yes! Perl can do anything that you can do in C or even ASM as far as bit manipulation goes...which means anything is possible!

    So this comes down to what you have "in" in terms of "bits" and what you want "out" in terms of "bits".

    There are some very cool Perl functions: pack and unpack that can do a magnificent job of translation.

    You will have to understand the difference between "little Endian" and "big Endian". These terms may seem strange as well as "binary coded decimal", or BCD.

    I have no idea what "packed decimal" means to you. You will have to post some input and desired output.

Re: Program to convert Numeric text file to Packed decimal file
by BrowserUk (Patriarch) on May 22, 2009 at 10:31 UTC

    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; }

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Program to convert Numeric text file to Packed decimal file
by dHarry (Abbot) on May 22, 2009 at 08:55 UTC