Need help in understanding conversion through REDEFINES



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

Need help in understanding conversion through REDEFINES

Postby ramkumar1992sp » Tue Aug 23, 2016 2:00 am

Hello Experts,

Can you please help me understand how the below REDEFINES works ?

When I step through the program,I find the values as below.

XXX-BYTES-01 = '{'
XXX-BYTES-02 = '0'

XXX-BYTES-BINARY = '49392'.

My question is how did we get '49392'.I looked at the below link and found that the Decimal value for EBCDIC '{' is 192 and decimal value of EBCDIC '0' is 240.

http://www.simotime.com/asc2ebc1.htm

Now can some help me in understanding how we get 49392 ?


01  XXX-BYTES.                                      
    05 XXX-BYTES-01             PIC X.              
    05 XXX-BYTES-02             PIC X.              
                                                     
01  XXX-BYTES-BINARY REDEFINES TWO-BYTES            
                              PIC 9(04) COMP-5.    
                                                     
 



Thanks,
Ram Kumar
ramkumar1992sp
 
Posts: 42
Joined: Sat Jul 23, 2016 8:52 am
Has thanked: 25 times
Been thanked: 0 time

Re: Need help in understanding conversion through REDEFINES

 

Re: Need help in understanding conversion through REDEFINES

Postby Robert Sample » Tue Aug 23, 2016 2:37 am

Contrary to your post title, REDEFINES does no conversion. REDEFINES uses the same storage for different variables. In your case, 192 time 256 plus 240 is 49,392. A COMP-5 variable uses the binary value of the bytes, in sequence, so for a two-byte field the first byte value is multiplied by 256 and added to the second byte value. The Enterprise COBOL Language Reference manual describes REDEFINES and the internal representation of numbers quite thoroughly.

These users thanked the author Robert Sample for the post:
ramkumar1992sp (Tue Aug 23, 2016 3:18 am)
Robert Sample
Global moderator
 
Posts: 3160
Joined: Sat Dec 19, 2009 8:32 pm
Location: East Dubuque, Illinois
Has thanked: 1 time
Been thanked: 190 times

Re: Need help in understanding conversion through REDEFINES

Postby ramkumar1992sp » Tue Aug 23, 2016 3:31 am

Thanks Robert,I read the language reference on REDEFINES and also on COMP items but I still find it harder to understand the not so straight forward REDEFINES (hope to get better at it) and also have questions of COMP items.I will be going back and reading it again.

How are we getting binary value of 192 (the first byte) by multiply it by 256 ? I didn't follow this.I know 2^8 is 256.

What if there were were 3 bytes,do we multiply the 1st byte with something else and then 2nd byte with 256 and add the third byte value ?

Can you please help me in clearing the confusion ?

Thanks,
Ram Kumar
ramkumar1992sp
 
Posts: 42
Joined: Sat Jul 23, 2016 8:52 am
Has thanked: 25 times
Been thanked: 0 time

Re: Need help in understanding conversion through REDEFINES

Postby Robert Sample » Tue Aug 23, 2016 9:03 am

In hexadecimal, 192 is X'C0' and 240 is X'F0'. Since these values are adjacent in your memory locations, you have a half-word (16-bit) value of X'C0F0'. This represents X'C' (12) times 16^3, 0 times 16^2, X'F' (15) times 16^1, and 0 times 16^0, which in turn represents 12 times 4096 plus 15 times 16 or 49152 plus 240. Many programmers pair hexadecimal digits (there are 4 bytes -- 8 hexadecimal digits -- per word on the system z architecture) and thus 192 times 256 plus 240. X'12345678' represents one full word of memory (PIC S9(09) COMP-5 in COBOL) and has a value of 1 x 16^7 + 2 x 16^6 + 3 x 16^5 + 4 x 16^4 (etc.) -- which could also be represented as X'12' (18) x 256^3 + X'34' (52) X 256^2 + X'56' (86) X 256 + X'78' (120).

The COBOL REDEFINES is a redefining of memory locations (one byte at the minimum). This means the REDEFINES variables do not occupy more memory locations, they occupy the SAME memory locations as the REDEFINED variable. So if you change either variable, the other variable takes on the same value -- they are sharing the memory location, not each having its own as is the usual case.

These users thanked the author Robert Sample for the post:
ramkumar1992sp (Tue Aug 23, 2016 9:52 am)
Robert Sample
Global moderator
 
Posts: 3160
Joined: Sat Dec 19, 2009 8:32 pm
Location: East Dubuque, Illinois
Has thanked: 1 time
Been thanked: 190 times

Re: Need help in understanding conversion through REDEFINES

Postby ramkumar1992sp » Tue Aug 23, 2016 9:55 am

Thank you so much Robert for taking time to explain this so clearly.

Thanks,
Ram Kumar
ramkumar1992sp
 
Posts: 42
Joined: Sat Jul 23, 2016 8:52 am
Has thanked: 25 times
Been thanked: 0 time

Re: Need help in understanding conversion through REDEFINES

Postby ramkumar1992sp » Thu Dec 08, 2016 12:47 am

Hello,

Sorry,for not starting a new topic but since its related to the above question.I thought of posting it here.
                                                     
01 WS-SEM-1                    PIC X(01).          
01 WS-SEM-2  REDEFINES WS-SEM-1 PIC 9(01) COMP-5.  



I need the decimal value of PIX X(01).It has { and I should be getting 192.

But when I redefined a comp-5 as above I was getting the value as 49152.

It looks like this is because PIC 9(01) occupies 2 bytes as per the below documentation and the decimal value is calculated as (192 * 256)

http://publibz.boulder.ibm.com/cgi-bin/ ... 0714120224

My question is how can we get just the decimal value of the character in PIC X(01) in an easier manner ?


Thanks
ramkumar1992sp
 
Posts: 42
Joined: Sat Jul 23, 2016 8:52 am
Has thanked: 25 times
Been thanked: 0 time

Re: Need help in understanding conversion through REDEFINES

Postby Robert Sample » Thu Dec 08, 2016 12:54 am

It looks like this is because PIC 9(01) occupies 2 bytes as per the below documentation and the decimal value is calculated as (192 * 256)
Uh -- NO! PIC 9(01) occupies 1 byte IF it is zoned decimal (or packed decimal). PIC 9(01) COMP-5 requires you to understand how COMP-5 (binary) values are stored in memory -- they will take 2, 4, or 8 bytes depending upon the number of digits in the PIC -- and PIC 9(01) COMP-5 requires 2 bytes. Possible ways around this issue include (but are not limited to):
01 WS-DATA.
    05                              PIC X(01) VALUE LOW-VALUES.
    05  WS-SEM-1                    PIC X(01).          
01 WS-SEM-2  REDEFINES WS-DATA PIC 9(01) COMP-5.  
or make WS-SEM-1 PIC 9(02) instead of PIC 9(01) or ....

One thing you MUST learn -- there is no conversion through REDEFINES. The source and REDEFINED variables have EXACTLY the same bits in the same positions; what you are doing with the REDEFINES is changing the way COBOL looks at them, you are not "converting" anything.

These users thanked the author Robert Sample for the post:
ramkumar1992sp (Thu Dec 08, 2016 1:11 am)
Robert Sample
Global moderator
 
Posts: 3160
Joined: Sat Dec 19, 2009 8:32 pm
Location: East Dubuque, Illinois
Has thanked: 1 time
Been thanked: 190 times

Re: Need help in understanding conversion through REDEFINES

Postby ramkumar1992sp » Thu Dec 08, 2016 1:12 am

Thanks Robert.I used the example that you showed and its working.I m sorry I left out COMP-5 while mentioning PIC 9(01).
ramkumar1992sp
 
Posts: 42
Joined: Sat Jul 23, 2016 8:52 am
Has thanked: 25 times
Been thanked: 0 time

Re: Need help in understanding conversion through REDEFINES

Postby Robert Sample » Thu Dec 08, 2016 3:22 am

Here's a bit of COBOL to show that there's no conversion involved in REDEFINES:
  05  WS-SOURCE-TEXT.                                            
       10                             PIC X(01) VALUE LOW-VALUES.
       10                             PIC X(03) VALUE 'pr('.      
   05  WS-SOURCE-COMP5                REDEFINES WS-SOURCE-TEXT    
                                      PIC S9(09) COMP-5.          
   05  WS-SOURCE-COMP3                REDEFINES WS-SOURCE-TEXT    
                                      PIC S9(07) COMP-3.          
   05  WS-DISPLAY-COMP5               PIC S9(10)                  
                                      SIGN LEADING SEPARATE.      
   05  WS-DISPLAY-COMP3               PIC S9(10)                  
                                      SIGN LEADING SEPARATE.      
                                                                 
PROCEDURE DIVISION.                                              
0000-MAIN.                                                        
    DISPLAY 'DISPLAY: ' WS-SOURCE-TEXT.                          
    DISPLAY 'COMP-5:  ' WS-SOURCE-COMP5.                          
    DISPLAY 'COMP-3:  ' WS-SOURCE-COMP3.                          
    MOVE WS-SOURCE-COMP5              TO  WS-DISPLAY-COMP5.      
    MOVE WS-SOURCE-COMP3              TO  WS-DISPLAY-COMP3.      
    DISPLAY 'COMP-5:  ' WS-DISPLAY-COMP5.                        
    DISPLAY 'COMP-3:  ' WS-DISPLAY-COMP3.                        
    STOP RUN.                                                    
                                                                 
which produces results of:
********************************* TOP OF DATA **
DISPLAY:  pr(                                  
COMP-5:  0009935181                            
COMP-3:  009799M                                
COMP-5:  +0009935181                            
COMP-3:  -0000097994                            
******************************** BOTTOM OF DATA
Notice that the first 3 DISPLAY statements show very different values -- even though I did nothing but REDEFINE variables. I put the MOVE statements in to make the COMP-5 and COMP-3 values more human-readable.

These users thanked the author Robert Sample for the post:
ramkumar1992sp (Thu Dec 08, 2016 4:09 am)
Robert Sample
Global moderator
 
Posts: 3160
Joined: Sat Dec 19, 2009 8:32 pm
Location: East Dubuque, Illinois
Has thanked: 1 time
Been thanked: 190 times

REDEFINES to change the way COBOL looks at data items.

Postby ramkumar1992sp » Thu Dec 08, 2016 8:00 pm

Hi Robert,

I didnt know till that I saw your code that we could leave the date item blank and not use filler.Thanks for it.

I looked at the below link and it says filler is optional.

http://publibz.boulder.ibm.com/cgi-bin/ ... 0821081020
ramkumar1992sp
 
Posts: 42
Joined: Sat Jul 23, 2016 8:52 am
Has thanked: 25 times
Been thanked: 0 time

Next

Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post