hex to ebcdic

R

roch77

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
 
G

Gunnar Hjalmarsson

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"

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 );
 
R

roch77

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..
 
D

Dr.Ruud

Gunnar Hjalmarsson schreef:
roch77:

There is a module.

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

See also Encode.
 
G

Gunnar Hjalmarsson

Dr.Ruud said:
Gunnar Hjalmarsson schreef:

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

Dr.Ruud

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,765
Messages
2,569,568
Members
45,042
Latest member
icassiem

Latest Threads

Top