Package 'convertBCD'

Title: Convert Decimal to Binary-Coded Decimal (BCD) Form and Vice Versa
Description: Convert BCD (raw bytes) to decimal numbers and vice versa. BCD format is used to preserve decimals exactly, as opposed to the binary rounding errors inherent in "numeric" or "floating-point" formats.
Authors: Carl Witthoft [aut, cre]
Maintainer: Carl Witthoft <[email protected]>
License: LGPL-3
Version: 1.0
Built: 2024-11-26 06:24:19 UTC
Source: CRAN

Help Index


Convert Decimal to Binary-Coded Decimal (BCD) Form and Vice Versa

Description

Convert BCD (raw bytes) to decimal numbers and vice versa. BCD format is used to preserve decimals exactly, as opposed to the binary rounding errors inherent in "numeric" or "floating-point" formats.

Details

The DESCRIPTION file:

Package: convertBCD
Type: Package
Title: Convert Decimal to Binary-Coded Decimal (BCD) Form and Vice Versa
Version: 1.0
Date: 2024-06-24
Authors@R: c(person(given = "Carl", family = "Witthoft", role = c("aut","cre"), email= "[email protected]"))
Description: Convert BCD (raw bytes) to decimal numbers and vice versa. BCD format is used to preserve decimals exactly, as opposed to the binary rounding errors inherent in "numeric" or "floating-point" formats.
License: LGPL-3
Imports: Rmpfr, gmp, methods
NeedsCompilation: no
Packaged: 2024-06-28 11:50:35 UTC; cgw
Author: Carl Witthoft [aut, cre]
Maintainer: Carl Witthoft <[email protected]>
Repository: CRAN
Date/Publication: 2024-06-28 15:00:02 UTC
Config/pak/sysreqs: libgmp3-dev libmpfr-dev

Index of help topics:

bcd2dec                 Function to convert binary-coded decimal (BCD)
                        data to decimal form. ~~
convertBCD-package      Convert Decimal to Binary-Coded Decimal (BCD)
                        Form and Vice Versa
dec2bcd                 Convert decimal numbers to binary-coded decimal
                        (BCD) form.  ~~ ~~

Author(s)

Carl Witthoft [aut, cre]

Maintainer: Carl Witthoft <[email protected]>


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"))

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.

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.

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.

Author(s)

Carl Witthoft, [email protected]

See Also

bcdToInteger dec2bcd

Examples

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

Convert decimal numbers to binary-coded decimal (BCD) form. ~~ ~~

Description

The input decimal values are converted to one or two vectors of class "raw" , representing an integer and optionally a decimal portion of a number.

Usage

dec2bcd(x, endian = c("little", "big"))

Arguments

x

Decimal numbers. Can be one of the following classes: numeric, bigz, character.

endian

The order of bytes desired in the raw BCD output.

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 are returned separately. .

Value

A list containing: intx , a vector of strings representing the integer parts of the decimal number decx, a vector of strings representing the decimal parts of the decimal number, if any.

Author(s)

Carl Witthoft, [email protected]

See Also

bcdToInteger , bcd2dec

Examples

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