IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Converting hex values stored in x(4) to 9(8).


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
shanudarling
Warnings : 1

New User


Joined: 20 Dec 2006
Posts: 55
Location: noida

PostPosted: Mon Dec 09, 2013 10:06 am
Reply with quote

Hi,
I have an existing file which has a x(4) field which contains date in hex format.
Sample values -
Code:

2102
0373

I want to to move this x(4) value into 9(8). For example, for the above sample the final expected value is 20130723.

I tried moving this value to s9(4) comp-3 but the value which moved was 0373.

Request you to please provide me some inputs to resolve this.

Thanks.
Back to top
View user's profile Send private message
Anuj Dhawan

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Mon Dec 09, 2013 11:50 am
Reply with quote

I'm on a toss but do you really mean what you say?

Edit: After a second thought, these topics should help you:

ibmmainframes.com/about51027.html
ibmmainframes.com/about48534.html
www-01.ibm.com/support/docview.wss?uid=swg21177358
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8697
Location: Dubuque, Iowa, USA

PostPosted: Mon Dec 09, 2013 5:30 pm
Reply with quote

You cannot do this with a MOVE statement -- you MUST write code to convert the hexadecimal values to decimal digits. Your code will take each hex character (the X'20', X'13', X'07' and X'23' in your example) and convert to decimal, accumulating the results as you go. The conversion can be done by a table or by EVALUATE or by arithmetic -- your choice -- but you will be converting base 16 values to base 10 values.
Back to top
View user's profile Send private message
Bill O'Boyle

CICS Moderator


Joined: 14 Jan 2008
Posts: 2501
Location: Atlanta, Georgia, USA

PostPosted: Mon Dec 09, 2013 7:25 pm
Reply with quote

Although I'm repeating what Anuj has posted, perhaps this is somewhat clearer -

Code:

     *                                                                 
     *     NOTE: ALL NUMERIC FIELDS MUST BE UNSIGNED                   
     *                                                                 
           03  WS-PACKED           PIC  9(09)      PACKED-DECIMAL.     
           03  WS-PACKED-V9        REDEFINES WS-PACKED                 
                                   PIC  9(08)V9    PACKED-DECIMAL.     
           03  WS-PACKED-X         REDEFINES WS-PACKED                 
                                   PIC  X(05).                         
           03  WS-DISPLAY          PIC  9(09).                         
           03  WS-DISPLAY-V9       REDEFINES WS-DISPLAY                 
                                   PIC  9(08)V9.                       
           03  WS-DISPLAY-X        REDEFINES WS-DISPLAY                 
                                   PIC  X(09).                         
      *                                                                 
           MOVE X'20130723'            TO WS-PACKED-X (1:4).           
           MOVE X'0F'                  TO WS-PACKED-X (5:).             
           MOVE WS-PACKED-V9           TO WS-DISPLAY-V9.               
      *                                                                 
           INSPECT WS-DISPLAY-X        CONVERTING X'FAFBFCFDFEFF'       
                                       TO 'ABCDEF'.                     
      *                                                                 
      *    WS-DISPLAY-X CONTAINS C'201307230'                           
      *                                                                 
           DISPLAY 'WS-DISPLAY-X = ', WS-DISPLAY-X.                     

Leave the INSPECT, in case there's another value you need to convert from HEX to CHARACTER, that may contain 4-Bit nibbles of "A" through "F", which you currently don't have in your example.

If your HEX value (for example) were X'20BF72A' then the INSPECT would be necessary to convert the unpacked nibbles of X'FB', X'FF' and X'FA, to 'B', 'F' and 'A', respectively.
Back to top
View user's profile Send private message
ApexNC

New User


Joined: 10 Feb 2006
Posts: 19
Location: USA

PostPosted: Wed Jan 08, 2014 5:36 am
Reply with quote

Based on the sample value given, I'd wager this is actually a YYYYMMDD field in Binary Coded Decimal format. In which case, the conversion to decimal is actually quite simple:

Code:

01  FILLER.                                       
    05 PIC-X-4 PIC X(4) VALUE X'20130723'.         
    05 PIC-9-8 PIC 9(8).                           
LINKAGE SECTION.                                   
01  BCD-CONV PIC 9(8)V9 COMP-3.                   
PROCEDURE DIVISION.                               
    SET ADDRESS OF BCD-CONV TO ADDRESS OF PIC-X-4 
    MOVE BCD-CONV TO PIC-9-8                       


If you don't mind RC=4 and a redefines length warning (IGYDS1154-W) on the compile, it can be made even simpler:

Code:

01  FILLER.                                         
    05 PIC-X-4 PIC X(4) VALUE X'20130723'.         
    05 BCD-CONV REDEFINES PIC-X-4 PIC 9(8)V9 COMP-3.
    05 PIC-9-8 PIC 9(8).                           
PROCEDURE DIVISION.                                 
    MOVE BCD-CONV TO PIC-9-8                                         


Putting BCD-CONV in LINKAGE and using SET ADDRESS gives RC=0 on the compile and avoids the redefines length warning.
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Wed Jan 08, 2014 6:38 am
Reply with quote

Requires documenting if used.

The definition overlaps the first byte of the following field, However, it doesn't matter, because nothing from that final byte is used.

The MOVE is to an integer, so the decimal part (V9), which is only there to allow the field to be defined as COMP-3/PACKED-DECIMAL, is not used.

The receiving field is unsigned, so the compiler will not use the sign from the source field, it will simply make the sign-nybble in the receiving field F.

So, it works.

However,

Code:
01  FILLER.
    05  BCD-VALUE-PLUS-ONE-BYTE.                                       
        10 PIC-X-4 PIC X(4) VALUE X'20130723'
        10 FILLER PIC 9 COMP-3 VALUE ZERO.         
    05 BCD-CONV REDEFINES BCD-VALUE-PLUS-ONE-BYTE PIC 9(8)V9 COMP-3.
    05 PIC-9-8 PIC 9(8).                           
PROCEDURE DIVISION.                                 
    MOVE BCD-CONV TO PIC-9-8


No messages, no wondering, no change of behaviour with a signed final receiver.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Issues Converting From ZD to Signed N... DFSORT/ICETOOL 4
No new posts INCLUDE OMIT COND for Multiple values... DFSORT/ICETOOL 5
No new posts Replace Multiple Field values to Othe... DFSORT/ICETOOL 12
No new posts Null values are considered in Total c... DFSORT/ICETOOL 6
No new posts Converting fixed length file to excel... IBM Tools 7
Search our Forums:

Back to Top