Subject: [Cbor] Proposal to add a CBOR tag for BCD

From: <[email-masked]>
Date: Tue, 28 Feb 2017 08:17:08 -0700

Here is the text of the markdown file attached:

# Proposal to add a CBOR tag for packed BCD

## Data item:

- Packed Binary Coded Decimal (BCD)

## Semantics:

- Encodes a sequence of base-10 digits in a sequence of bytes, each containing one digit

in its high nybble (4 bits) and the next less-significant digit in its low nybble.

- A value of 15 in the high nybble of the first byte in the sequence indicates a negative number.

- A value of 15 in the low nybble of the last byte in the sequence is simply discarded.

- A value between 10 and 14 in any nybble indicates that the preceding digit is repeated

between 2 and 6 additional times.

## Background

[Packed binary-coded decimal (BCD)](https://en.wikipedia.org/wiki/Binary-coded_decimal)

in its various forms is a somewhat outdated way to code a sequence of

base-10 digits. It has largely been replaced in decimal arithmetic by

[Mike Cowlishaw's work at IBM](http://speleotrove.com/decimal/), particularly his

more compact [densely packed decimal (DPD)](https://en.wikipedia.org/wiki/Densely_packed_decimal)

that is specified in IEEE 754 for encoding the significand or mantissa of decimal number.

DPD is about 20% more compact than BCD, but BCD has a few advantages:

- Encoding and decoding BCD in a program is trivially easy, whereas DPD is

quite a bit more complex.

- Encoding and decoding BCD is extremely efficient, and takes very little processor cycles.

- BCD values are human readable in a decoding situation.

My immediate use case is to encode and decode standard decimal numbers in CBOR in a native

Python program. What I have easy access to for the significand is an array of decimal digits.

BCD seems like the best way to encode this in my application, and it occurred to me that CBOR

might benefit by providing a tag specifically for BCD (hopefully in the range 24-255).

In addition, a tag for DPD would make sense, but has less interest to me, since where decimals

are easily accessible in fixed-length formats, they can go over the wire in binary form.

Above, I describe BCD semantics that differ slightly from any of the classic BCD forms,

using otherwise wasted space to provide rudimentary run-length encoding.