pub fn encode_zoned_decimal(
value: &str,
digits: u16,
scale: i16,
signed: bool,
codepage: Codepage,
) -> Result<Vec<u8>>Expand description
Encode a zoned decimal using the configured code page defaults.
Encodes decimal values to zoned decimal format (PIC 9) where each digit is stored in a byte with a zone nibble and a digit nibble. For signed fields, the last byte uses overpunch encoding for the sign.
§Arguments
value- String representation of the decimal value to encodedigits- Number of digit characters (field length)scale- Number of decimal places (can be negative for scaling)signed- Whether the field is signed (true) or unsigned (false)codepage- Character encoding (ASCII or EBCDIC variant)
§Returns
A vector of bytes containing the encoded zoned decimal
§Policy
Applies ZeroSignPolicy::Positive for ASCII and ZeroSignPolicy::Preferred for EBCDIC when no overrides are provided.
§Errors
Returns an error if the value cannot be encoded as a zoned decimal with the specified parameters.
§Examples
§Basic ASCII Encoding
use copybook_codec::numeric::{encode_zoned_decimal};
use copybook_codec::options::Codepage;
// Encode "123" as ASCII zoned decimal
let encoded = encode_zoned_decimal("123", 3, 0, false, Codepage::ASCII)?;
assert_eq!(encoded, b"123"); // [0x31, 0x32, 0x33]§Signed ASCII Encoding (Overpunch)
use copybook_codec::numeric::{encode_zoned_decimal};
use copybook_codec::options::Codepage;
// Encode "-456" with overpunch sign
let encoded = encode_zoned_decimal("-456", 3, 0, true, Codepage::ASCII)?;
// Last byte 0x4D = 'M' = digit 3 with negative sign
assert_eq!(encoded, [0x34, 0x35, 0x4D]);§EBCDIC Encoding
use copybook_codec::numeric::{encode_zoned_decimal};
use copybook_codec::options::Codepage;
// Encode "789" as EBCDIC zoned decimal
let encoded = encode_zoned_decimal("789", 3, 0, false, Codepage::CP037)?;
assert_eq!(encoded, [0xF7, 0xF8, 0xF9]);§Decimal Scale
use copybook_codec::numeric::{encode_zoned_decimal};
use copybook_codec::options::Codepage;
// Encode "12.34" with 2 decimal places
let encoded = encode_zoned_decimal("12.34", 4, 2, false, Codepage::ASCII)?;
assert_eq!(encoded, b"1234"); // [0x31, 0x32, 0x33, 0x34]§See Also
encode_zoned_decimal_with_format- For encoding with explicit formatencode_zoned_decimal_with_format_and_policy- For encoding with format and policyencode_zoned_decimal_with_bwz- For encoding with BLANK WHEN ZERO supportdecode_zoned_decimal- For decoding zoned decimals