hex to ebcdic

Discussion in 'Perl Misc' started by roch77@gmail.com, Jun 29, 2007.

  1. Guest

    hi,
    I have a string of hex that represent ebcdic characters. Is there a
    way that I can do the following in perl.


    hex string = "f1f2d7d9f0"

    the result I want = "12PR0"

    >From looking up a ascii/hex/ebcdic table, I was able to arrive at the

    result. ie: f1 => 1, f2 => 2, d7 =>P etc..

    Is there a perl function that will do this?
    (I don't want to code the entire lookup table if I don't have to).

    I am doing this in linux if that matters.

    thanks
    , Jun 29, 2007
    #1
    1. Advertising

  2. wrote:
    > I have a string of hex that represent ebcdic characters. Is there a
    > way that I can do the following in perl.
    >
    > hex string = "f1f2d7d9f0"
    >
    > the result I want = "12PR0"
    >
    >>From looking up a ascii/hex/ebcdic table, I was able to arrive at the

    > result. ie: f1 => 1, f2 => 2, d7 =>P etc..
    >
    > Is there a perl function that will do this?


    There is a module.

    use Convert::EBCDIC 'ebcdic2ascii';
    $hex = 'f1f2d7d9f0';
    ( $ebcdic = $hex ) =~ s/(..)/chr(hex $1)/eg;
    $ascii = ebcdic2ascii( $ebcdic );

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jun 29, 2007
    #2
    1. Advertising

  3. Guest

    Thanks a lot.
    I have Convert::EBCDIC, but didn't quite figure out how to do it.

    Actually the file that I am ftping from the mainframe has packed
    numeric (comp.) as well as these display fields. So thats why I do a
    binary mode ftp.

    Then I use "unpack "H*", $buf" and all the display fields show up as
    hex representation of EBCDIC.
    So I will use Gunner's suggestion to convert them.. There maybe other
    ways like the convertibm390 module as well I guess..

    Thanks very much once again..

    > use Convert::EBCDIC 'ebcdic2ascii';
    > $hex = 'f1f2d7d9f0';
    > ( $ebcdic = $hex ) =~ s/(..)/chr(hex $1)/eg;
    > $ascii = ebcdic2ascii( $ebcdic );
    >
    > --
    > Gunnar Hjalmarsson
    > Email:http://www.gunnar.cc/cgi-bin/contact.pl
    , Jun 30, 2007
    #3
  4. Dr.Ruud Guest

    Gunnar Hjalmarsson schreef:
    > roch77:


    >> I have a string of hex that represent ebcdic characters. Is there a
    >> way that I can do the following in perl.
    >>
    >> hex string = "f1f2d7d9f0"
    >>
    >> the result I want = "12PR0"
    >>
    >>> From looking up a ascii/hex/ebcdic table, I was able to arrive at
    >>> the

    >> result. ie: f1 => 1, f2 => 2, d7 =>P etc..
    >>
    >> Is there a perl function that will do this?

    >
    > There is a module.
    >
    > use Convert::EBCDIC 'ebcdic2ascii';
    > $hex = 'f1f2d7d9f0';
    > ( $ebcdic = $hex ) =~ s/(..)/chr(hex $1)/eg;
    > $ascii = ebcdic2ascii( $ebcdic );


    See also Encode.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Jun 30, 2007
    #4
  5. Dr.Ruud wrote:
    > Gunnar Hjalmarsson schreef:
    >> roch77:
    >>> I have a string of hex that represent ebcdic characters. Is there a
    >>> way that I can do the following in perl.
    >>>
    >>> hex string = "f1f2d7d9f0"
    >>>
    >>> the result I want = "12PR0"
    >>>
    >>> From looking up a ascii/hex/ebcdic table, I was able to arrive at
    >>> the result. ie: f1 => 1, f2 => 2, d7 =>P etc..
    >>>
    >>> Is there a perl function that will do this?

    >>
    >> There is a module.
    >>
    >> use Convert::EBCDIC 'ebcdic2ascii';
    >> $hex = 'f1f2d7d9f0';
    >> ( $ebcdic = $hex ) =~ s/(..)/chr(hex $1)/eg;
    >> $ascii = ebcdic2ascii( $ebcdic );

    >
    > See also Encode.


    Right, making use of a standard module is more convenient.

    use Encode 'decode';
    $hex = 'f1f2d7d9f0';
    ( $ebcdic = $hex ) =~ s/(..)/chr(hex $1)/eg;
    $ascii = decode 'posix-bc', $ebcdic;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jun 30, 2007
    #5
  6. Dr.Ruud Guest

    Gunnar Hjalmarsson schreef:

    > use Encode 'decode';
    > $hex = 'f1f2d7d9f0';
    > ( $ebcdic = $hex ) =~ s/(..)/chr(hex $1)/eg;
    > $ascii = decode 'posix-bc', $ebcdic;


    Ah, "posic-bc", brings back memories. I would've thought that "EBCDIC"
    or "EBCDIC-US" (see `iconv -l`) would work too, but they don't, see
    `perldoc Encode::Supported`.

    Variant using pack()

    $ perl -Mstrict -MEncode=decode -wle'
    my $ebcdic = pack q/H*/, $ARGV[0];
    my $ascii = decode q/posix-bc/, $ebcdic;
    print $ascii;
    ' f1f2d7d9f0
    12PR0

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Jun 30, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,110
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    439
    Juha Autero
    Aug 19, 2003
  3. jack
    Replies:
    4
    Views:
    566
  4. tim

    hex string to hex value

    tim, Nov 22, 2005, in forum: Python
    Replies:
    8
    Views:
    18,765
  5. tim
    Replies:
    2
    Views:
    1,522
    Dennis Lee Bieber
    Nov 23, 2005
Loading...

Share This Page