1832541 Members
5322 Online
110043 Solutions
New Discussion

Re: Hex Help

 
SOLVED
Go to solution
cbres00
Frequent Advisor

Hex Help

Friends,
I need to produce a file on unix for a mainframe application.

The specs for the file say that each record
of the file needs to be started with
"a delimiter of ¬ which translates to hex value of X'5F'. Each record is terminated by a 2-byte field with the hex value of x'0D25'."

What's a nice, clean solution for this?

I've toyed with bc but haven't had much success. Any thoughts? Thx in advance.
Cat
Life is too short not to have fun every single day
14 REPLIES 14
cbres00
Frequent Advisor

Re: Hex Help

Ok...I've done more research. I know I need to use the dd command with a conv=ibm option, but I'm not sure how to achieve my goal. Man pages aren't terribly helpful for what I want to do. :-(
Life is too short not to have fun every single day
Hein van den Heuvel
Honored Contributor

Re: Hex Help


I'd use perl. To get 'hex' constants check out sprintf, vec anc pack. For example:
http://www.perldoc.com/perl5.8.0/pod/func/vec.html

Here is a possible solution:

perl -e 'vec($h,0,8)=0x5F; vec($t,0,16)=0x0D25; while(<>) {chop; print $h,$_,$t}' < unixfile > mainframefile

check with: od -t x1 file | head

hth,
Hein.


Elmar P. Kolkman
Honored Contributor

Re: Hex Help

For more help I'm afraid I can't help very much, apart from a way to have create those characters. For the 0x5F and the 0x0D25 you could use something like this in awk (or C):
printf("%c",95); # for the 0x5f, which is 95
printf("%c%c",13,37); # for the 0x0D25

I'm not sure what else you need.
Every problem has at least one solution. Only some solutions are harder to find.
Graham Cameron_1
Honored Contributor
Solution

Re: Hex Help

Elmer is in thw right lines, but you need to use %x for hex, not %c (which is character).
Also the mainframe will probably expect each hex value to be 2 chars wide, so you need a precision of 2.2 also.

ie from awk, you would use
printf("%-2.2x",95); # for the 0x5f, which is 95
printf ("THE REST OF YOUR RECORD HERE")
printf("%-2.2x%-2.2x\n",13,37); # for the 0x0D25

Hope this helps.
(BTW, I don't think bc is the way to go)

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Alan Turner
Regular Advisor

Re: Hex Help

Cat

It might be a good idea to check the hex values again, because none of these values is unusual in ASCII:

hex 5F is an underscore character
hex 0D is carriage return [e.g. printf("\r")]
hex 25 is a percent character.

Also, check the order of the bytes in the 2-byte field value x'0D25'. On a big-endian machine like PA-RISC or SPARC, the first byte in a multi-byte value is the most sigificant, so a 2-byte value of 0D25 would be obtained by a byte 0D followed by a byte 25, e.g. printf("\r%%"). However, on a little-endian machine, such as 80x86, Alpha, or VAX, the first byte in a multi-byte value is the least significant, so the 2-byte value of 0D25 would be obtained by a percent character followed by a carriage return character, e.g. printf("%%\r").

Best to have a look at the records on your destination machine.

If you need to convert to EBCDIC, dd is an obvious solution. I'd have expected PERL to have an ASCII/EBCDIC converter, but a (very) quick scan of my PERL book didn't come up with anything.

Good Luck.
cbres00
Frequent Advisor

Re: Hex Help

I think Graham's solution may be the best and simplest. Can you kindly give me the full awk syntax?
Life is too short not to have fun every single day
Graham Cameron_1
Honored Contributor

Re: Hex Help

cbres00

Are you the same person as Cathy Breslow ?

Why 2 ids ?

Anyway I (or anyone - I go home in 10 minutes) can have a go at giving you "the full awk syntax", but we would need a few more clues.

Like what's your input data?
What's your output format?
Anything happen to the data in between?

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
cbres00
Frequent Advisor

Re: Hex Help

Sorry, that's me! I logged on as Cathy Breslow last night....

I want to create a variable with these hex values because I need to insert them in several places in the file.

Cathy
Life is too short not to have fun every single day
A. Clay Stephenson
Acclaimed Contributor

Re: Hex Help

I would tend towards Perl for this especially if you also have to do ASCII/EBCDIC conversion. There is a Convert::EBCDIC module that will do the conversion and unlike dd, it's not all or nothing.

Here's a thread that discusses this:


http://forums1.itrc.hp.com/service/forums/parseCurl.do?CURL=%2Fcm%2FQuestionAnswer%2F1%2C%2C0xefcfd7d96cbad711900a0090279cd0f9%2C00.html&admit=716493758+1067531246041+28353475

I also noted that the recent Foruns upgrade mangled some of my syntax so I fikx and attach the original convert.pl script. Adding hard-coded hex values toi the output is trivially easy.

If it ain't broke, I can fix that.
Hein van den Heuvel
Honored Contributor

Re: Hex Help


> I think Graham's solution may be the best and simplest

I'm afraid it is not. It just outputs the hex as text, not as a hex value in a character. It is much similar to: print "5F", $0, "0D25", $0;

Be sure to re-view the solution I suggested.
It has the header and tail in variables $h and $t.

Also be sure to study the comments in other replies on EBCDIC, 'normal characters' being recognized as terminators, and the endianness. (For example, in my solution you may have to swap the 0D and 25 in the string


Good luck,
Hein.
Michael Schulte zur Sur
Honored Contributor

Re: Hex Help

Hi,

in case you haven´t got a solution yet.
Otherwise be so kind to award points to the contributors, which helped you.

Michael

#!/bin/ksh
while read TEXT
do
echo "\0137${TEXT}\015\045\c"
done < $
cbres00
Frequent Advisor

Re: Hex Help

How do I assign points?
Life is too short not to have fun every single day
Michael Schulte zur Sur
Honored Contributor

Re: Hex Help

Hi,

you would have to login as Cathy since that is the user, that created the post. Then you see drop down buttons with 0 to 10 points.
Click on how many points you want to award, and use the submit points button at the buttom.

greetings,

Michael
Joshua Scott
Honored Contributor

Re: Hex Help

You'll have to login as 'Cathy Breslow' to assign points. points can only be assigned by the original user who posted the Question.
What are the chances...