Unpack routine from Steve Pitts (see EMail
Addresses)
Captured from a message in a public CompuServe Forum
/* ------------------------------------------------------------------ */
/* function: unpack routine for packed data (from mainframes) */
/* */
/* call: unpack packed_str {,num_dec} */
/* */
/* where: packed_str = the packed data */
/* num_dec = number of decimals (def.: 0) */
/* */
/* */
/* returns: the unpacked number or "" in case of an error */
/* */
Unpack: PROCEDURE
parse arg packed_str, num_dec
if num_dec = "" then
num_dec=0
/* Convert packed data to hex and split into */
/* number and sign portions */
hex_str=c2x( packed_str )
dec_str=left( hex_str, length( hex_str )-1 )
packed_sign=right( hex_str, 1 )
/* Check that sign and numeric portions have */
/* valid values */
if verify( packed_sign, "ABCDEF" ) > 0 then
return ""
if verify( dec_str, "0123456789" ) > 0 then
return ""
/* Are there enough digits for the decimal */
/* point?? */
if num_dec > length( dec_str ) then
return ""
/* If sign portion indicates a negative */
/* number then oblige */
if pos( packed_sign, "BD" ) > 0 then
dec_str=0-dec_str
/* If there is a decimal point then add it at */
/* the appropriate place */
if num_dec > 0 then
dec_str=insert( ".", dec_str, length( dec_str )-num_dec )
RETURN dec_str