Intrinsic function to convert numeric to string

Asked

Viewed 2,308 times

10

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

(    PIC S9(04) COMP) 

If yes, it is more expensive than using the REDEFINES?

  • 5

    COBOL is off-topic here... : D Prankster, huh folks? Won’t vote to close. It was missing diversity even!

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) usually it is 0-9999. It is possible to use all bits: does O-NUMERO PIC 9(5).

Or in the 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 old language. It was not designed for functions. Until 1989 it had no functions. As of 1989 there is a limited list of intrinsic functions (intrinsic). They are part of the language and exclusively processed by the compiler (aided by various runtime 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, see GNU COBOL (search for them on Sourceforge).

So, I’m sorry, but you’ll have to manage without functions.

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

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

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

1

Having two variables:

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, no function required.

There is also the option to declare the numeric variable as daughter.

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

Thus, the variable ALPHA-NUMERICO contains the value of NUMERICO in string form.

  • COBOL has no strings. The "conversion" you are proposing contains no conversion at all, a signal-free USAGE DISPLAY numeric field has the exact bit-settings as an alphanumeric USAGE DISPLAY field that contains the equivalent numeric value as its second example shows. Asker has a binary field, and seems to want the text representation of its hexadecimal value. However, Asker never came back, so who knows ...

  • I didn’t notice that the numeric field is binary... in this case, my example doesn’t really fit. And you can’t convert directly, not even with REDEFINES, due to the sign. I believe his intention would be to have something like "+1234" in an alphanumeric field.

  • We may never know what it was we really wanted :-)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.