PCK() Function - Pack a Number

Syntax

PCK(num1,num2{,ERR=lineref})

Description

The PCK() function (and the counterpart function UPK()) has been added for compatibility with older Business BASICs that support these nonstandard capabilities. In the syntax example above, num1 is any numeric expression, and num2 is an integer from 1 to 6. The string result is num2 bytes long. The packed string may hold two digits per byte; therefore, a 6-byte packed string may hold a 12-digit number (including a sign). If num1 has any fractional digits, they are ignored. If the value is too large for the size of the packed result, excess digits on the left are discarded.

The intent of PCK() and UPK() is to allow compression of numeric values into a pseudo-binary form to save disk space. Because the BIN() and CHR() functions generate bytes that could be mistaken for field terminators (by the READ statement), another method was needed.

PCK() and UPK() are designed so that the packed strings do not contain field terminators. To be consistent with other implementations of PCK()/UPK(), the packed string for negative values will compare greater than a packed string for positive values. Also, a packed string for -2 will compare greater than a packed string for -1, though, numerically, -1 is greater than -2. PRO/5 does not necessarily use the same packed format as other BASICs; therefore, when converting existing data files to PRO/5, packed fields must be unpacked and then repacked under PRO/5.

Examples

>A$=PCK(12345678,4)
>PRINT A$
,BXn
>PRINT LEN(A$)
4
PRINT UPK(A$)
12345678

See Also

UPK() Function

Functions - Alphabetical Listing