Learn R Programming

convertBCD (version 1.0)

bcd2dec: Function to convert binary-coded decimal (BCD) data to decimal form. ~~

Description

Given one or two input vectors of class "raw" , representing an integer and optionally a decimal portion of a number, the decimal values are returned.

Usage

bcd2dec(x, xdec = NULL, mergex = TRUE, endian = c("little", "big"))

Value

A list with elements: intx , character strings representing the decimal numbers produced or, if mergex == FALSE, the integer portions of the numbers. decx , set to NULL unless mergex == FALSE, in which case character strings representing the decimal portions of the numbers generated.

Arguments

x

The vector of bytes containing the BCD representation of the integer portion of a value. Must be of class 'raw'.

xdec

The vector of bytes containing the BCD representation of the decimal portion of a value Must be of class 'raw'. The default is NULL.

mergex

A logical value. If TRUE, The integer and decimal parts are combined to return a single number. Otherwise the integer and decimal parts are returned separately.

endian

The order of bytes in the raw BCD input.

Author

Carl Witthoft, carl@witthoft.com

Details

The BCD format reserves a full byte for each character (number) in an input value. While this is memory-expensive, it guarantees the exact value is stored, unlike class numeric or others, which are subject to binary expansion rounding errors. There is no standard for indicating the location of a decimal point in BCD data, which is why the integer and decimal portion must be entered separately here.

See Also

bcdToInteger dec2bcd

Examples

Run this code
foo <- dec2bcd('37.852')
bar <- bcd2dec(foo$xint[[1]],foo$xdec[[1]])

Run the code above in your browser using DataLab