Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > hex to ebcdic

Reply
Thread Tools

hex to ebcdic

 
 
roch77@gmail.com
Guest
Posts: n/a
 
      06-29-2007
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

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-29-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
roch77@gmail.com
Guest
Posts: n/a
 
      06-30-2007
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



 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      06-30-2007
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."
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-30-2007
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
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      06-30-2007
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."

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hex string to hex value tim Python 8 11-23-2005 06:27 PM
[Fwd: Re: hex string to hex value] tim Python 2 11-23-2005 07:18 AM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
hex value in string back to real hex value jack Python 4 09-08-2004 07:11 AM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments