Convert char to hex



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Convert char to hex

Postby aimin8828 » Wed Apr 17, 2013 6:02 pm

Dear all:

There was a question as below in my interview, could you please help to have a look how to achieve it? thanks a lot!

Question: How to convert the char variable with 2 bytes into HEX variable with 1 byte? (The input characters is just '0123456789ABCDEF'), for exeample, input is 'AB' and the output is X'AB'.
aimin8828
 
Posts: 9
Joined: Fri Mar 01, 2013 6:40 pm
Has thanked: 1 time
Been thanked: 0 time

Re: Convert char to hex

Postby Robert Sample » Wed Apr 17, 2013 6:30 pm

Method 1:
Use EVALUATE with 256 WHEN statements specified

Method 2:
Use a PIC S9(04) COMP-5 output variable
Redefine the output variable so you can access each byte separately
Use EVALUATE with 16 WHEN statements for the first byte of the input and compute the COMP-5 variable appropriately
Use EVALUATE with 16 WHEN statements for the second byte of the input and compute the COMP-5 variable appropriately
Move the redefined second byte of the COMP-5 variable to your final variable

Method 3:
Use the intrinsic function ORD with appropriate IF logic to convert the bytes of the input variable

These are suggestions -- there are other ways to do the conversion.
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Convert char to hex

Postby BillyBoyo » Wed Apr 17, 2013 6:35 pm

Various ways. For instance, each character represents the values 0-15. 0-9 map to 0-9, A-F map to 10-15.

The first digit is 16 times bigger (number base is 16, remember) than the one on the right.

Add 16 * "value from first character" to "value from second character" storing the result in a binary/comp/comp-4/comp-5 field of two bytes. Redefine the field so that the rightmost byte is PIC X.

DISPLAY that rightmost byte. Presto.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: Convert char to hex

Postby aimin8828 » Wed Apr 17, 2013 6:53 pm

BillyBoyo wrote:Various ways. For instance, each character represents the values 0-15. 0-9 map to 0-9, A-F map to 10-15.

The first digit is 16 times bigger (number base is 16, remember) than the one on the right.

Add 16 * "value from first character" to "value from second character" storing the result in a binary/comp/comp-4/comp-5 field of two bytes. Redefine the field so that the rightmost byte is PIC X.

DISPLAY that rightmost byte. Presto.



Dear Robert / BillyBoyo ,Thank you very much for your kind response.
aimin8828
 
Posts: 9
Joined: Fri Mar 01, 2013 6:40 pm
Has thanked: 1 time
Been thanked: 0 time

Re: Convert char to hex

Postby Anuj Dhawan » Tue Apr 30, 2013 6:40 pm

I'm late to the party but I'm beaten up by the statement
for exeample, input is 'AB' and the output is X'AB'.
- did it mean what it says? :? Please assist.
Anuj
Anuj Dhawan
 
Posts: 273
Joined: Mon Feb 25, 2008 3:53 am
Location: Mumbai, India
Has thanked: 6 times
Been thanked: 4 times

Re: Convert char to hex

Postby Robert Sample » Tue Apr 30, 2013 7:04 pm

Anuj:

Input would be X'C1C2' ('AB') and output would be X'AB'
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Convert char to hex

Postby Anuj Dhawan » Wed May 01, 2013 9:58 am

That make sense, Thanks Robert.

Thanks for saving me from forgetting all what I learned about HEXADECIMAL during college...! :D
Anuj
Anuj Dhawan
 
Posts: 273
Joined: Mon Feb 25, 2008 3:53 am
Location: Mumbai, India
Has thanked: 6 times
Been thanked: 4 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post