Intrinsic function to convert numeric to string

10

I'm trying to find out if there is any intrinsic COBOL function to convert a numeric date to string without having to use the REDEFINES clause:

(    PIC S9(04) COMP) 

If yes, is it more costly to use REDEFINES ?

    
asked by anonymous 30.01.2014 / 13:39

2 answers

10

No, it has no intrinsic function. REDEFINES , directly, does nothing for you.

If you want the numbers:

01  O-CAMPO COMP PIC 9(4).

01  O-NUMERO PIC 9(4).

MOVE O-CAMPO TO O-NUMERO
DISPLAY O-NUMERO

X'000F' -- 0015

Note that, PIC 9(4) is usually 0-9999 . It is possible to use all bits: it does O-NUMERO PIC 9(5) .

Or, in Hex:

01  O-CAMPO COMP PIC 9(4).
01  FILLER REDEFINES O-CAMPO.
    05  O-CAMPO-HIGH PIC X. 
    05  O-CAMPO-LOW  PIC X. 

01  HEX-SUBSCRIPT COMP PIC 9(4) VALUE ZERO.
01  FILLER REDEFINES HEX-SUBSCRIPT.
    05  FILLER PIC X. 
    05  HEX-SUBSCRIPT-LOW PIC X.

01  HEX-IN-TEXT.
    05  HEX-IN-TEXT-HIGH PIC XX.
    05  HEX-IN-TEXT-LOW PIC XX.

01  HEX-DIGIT-TABLE.
    05  FILLER PIC X(32) 
               VALUE '000102030405060708090A0B0C0D0E0F'.
    05  FILLER PIC X(32) 
               VALUE '101112131415161718191A1B1C1D1E1F'.
    05  FILLER PIC X(32) 
               VALUE '202122232425262728292A2B2C2D2E2F'.
    05  FILLER PIC X(32) 
               VALUE '303132333435363738393A3B3C3D3E3F'.
    05  FILLER PIC X(32) 
               VALUE '404142434445464748494A4B4C4D4E4F'.
    05  FILLER PIC X(32) 
               VALUE '505152535455565758595A5B5C5D5E5F'.
    05  FILLER PIC X(32) 
               VALUE '606162636465666768696A6B6C6D6E6F'.
    05  FILLER PIC X(32) 
               VALUE '707172737475767778797A7B7C7D7E7F'.
    05  FILLER PIC X(32) 
               VALUE '808182838485868788898A8B8C8D8E8F'.
    05  FILLER PIC X(32) 
               VALUE '909192939495969798999A9B9C9D9E9F'.
    05  FILLER PIC X(32) 
               VALUE 'A0A1A2A3A4A5A6A7A8A9AAABACADAEAF'.
    05  FILLER PIC X(32) 
               VALUE 'B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF'.
    05  FILLER PIC X(32) 
               VALUE 'C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF'.
    05  FILLER PIC X(32) 
               VALUE 'D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF'.
    05  FILLER PIC X(32) 
               VALUE 'E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF'.
    05  FILLER PIC X(32) 
               VALUE 'F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF'.
01 FILLER REDEFINES HEX-DIGIT-TABLE.
    05 HEX-DIGITS  PIC X(2) OCCURS 256. 


    MOVE O-CAMPO-HIGH TO HEX-SUBSCRIPT-LOW
    MOVE HEX-DIGITS ( HEX-SUBSCRIPT + 1 ) TO HEX-IN-TEXT-HIGH
    MOVE O-CAMPO-LOW TO HEX-SUBSCRIPT-LOW
    MOVE HEX-DIGITS ( HEX-SUBSCRIPT + 1 ) TO HEX-IN-TEXT-LOW
    DISPLAY HEX-IN-TEXT

X'000F' -- 000F

COBOL is an ancient language. It was not designed for functions. Until 1989 she had no job. As of 1989 there is a limited list of intrinsic functions (% with%). They are part of the language and are exclusively processed by the compiler (aided by several run-time routines, mainly). There are no user-defined functions. There will be more intrinsic functions in the next version of the COBOL standard.

User-defined functions will exist in COBOL in the future. If you want to use them now, check out GNU COBOL (search for it at SourceForge).

So, I'm sorry, but you're going to have to run out of functions.

REDEFINES does not do what you seem to think it does. It only allows another "mapping" of a predefined storage. The content of the storage is not affected in any way, and retains the original value until you use the REDEFINES item as target of an instruction.

In COBOL, you have to coddle in the hand. What I explained above is one way. In terms of execution speed and PROCEDURE lines of code, it is a good way. It requires a table to be defined in WORKING-STORAGE (or possibly in LOCAL-STORAGE).

COBOL (default) can not handle individual bytes in binary. The technique is to reset (REDEFINES) a two-byte binary field with a ZERO initial VALUE, and then place an individual byte in the least order byte, and use the binary field as subscript . Since you want to convert X'00 'to' 00 ', and zero is not a valid subscript, but needs to be the first table entry, you need to move the subscript reference into one (HEX-SUBSCRIPT + 1). If you prefer, you can use ADD / COMPUTE to increment the value of the subscript in one.

    
30.01.2014 / 17:58
1

Having two variants:

01 ALPHA-NUMERICO PIC X(10) VALUE SPACES.
01 NUMERICO       PIC 9(10) VALUE 1234567890.

A simple command MOVE NUMERICO TO ALPHA-NUMERICO already converts to string, with no need for a function.

There is also the option to declare the numeric variable as a child.

01 ALPHA-NUMERICO.
  03 NUMERICO       PIC 9(10) VALUE 1234567890.

In this way, the variable ALPHA-NUMERICO contains the value of NUMERICO in the form of string.

    
13.06.2014 / 22:44