|
|||||||||||
|
Chicago-Soft
|
Working With Binary Integer Data in REXX by Steve Comstock In an article in Fall 2004 TSO Times (Working With Packed-Decimal In REXX), I discussed how to extract the zoned-decimal value from packed-decimal items in REXX. This article will provide an example of working with a file that contains [at least] one binary integer field. Just as in the packed-decimal article, the assumption is made that you know how to read and write records, using record I/O (EXECIO) or stream I/O (linein, charin, linout, charout, and so on), so the focus here will be on the actual processing of binary numbers. Recall also that binary integer data is numeric data stored as powers of two, typically in 2 bytes, 4 bytes, or 8 bytes. Suppose there is an inventory file containing 100-byte records and that locations 41-44 contain the quantity on hand for an item in a warehouse. If there is a value there of X'00000032', this indicates that there are 50 of these items in stock. (X'32' = 3 X 16 + 2 in decimal). Suppose that an ISPF dialog driven by a REXX exec takes online orders and that a customer places a request to buy some quantity of a particular item, say 12 Wacky Widgets. The REXX code needs to determine if there is an adequate number on hand to satisfy the customer's request. Assume that the quantity ordered is in the REXX variable named qty and the item description is in a field called desc. The REXX EXEC is able to locate the relevant inventory record and read it into a variable called inrecord. The next step is to convert the data in positions 41-44 to REXX-type numeric data. The tool to use here is the REXX built-in function named C2D ("Character-to-Decimal"). This function takes any string as input and treats the digits as a binary number, returning a decimal value. For example, C2D('b') would return the value 130, since a character 'b' is X'82'. In the inventory example, the conversion code would look something like this:
Note the use of the D2C built-in function to convert a decimal number back to binary. D2C functions exactly opposite of C2D. Also note the use of the REXX overlay function to replace the binary data at positions 41-44 within inrecord. C2D and D2C, as well as C2X discussed in the packed-decimal article, are somewhat counter-intuitive in how they work. It is important to test and experiment a lot when using them. But these are the functions that enable the processing of binary integer and packed-decimal fields in REXX EXECs.
|
|
||||||||||
home · current
articles
· archives · forums · |