Skip to main content

encode_alphanumeric

Function encode_alphanumeric 

Source
pub fn encode_alphanumeric(
    text: &str,
    field_len: usize,
    codepage: Codepage,
) -> Result<Vec<u8>>
Expand description

Encode an alphanumeric (PIC X) field with right-padding to the declared length.

Converts the UTF-8 input string to the target codepage encoding and then right-pads with space characters (ASCII 0x20 or EBCDIC 0x40) to fill the declared field length.

§Arguments

  • text - UTF-8 string value to encode
  • field_len - Declared byte length of the COBOL field
  • codepage - Target character encoding (ASCII or EBCDIC variant)

§Returns

A vector of exactly field_len bytes containing the encoded and padded text.

§Errors

  • CBKE501_JSON_TYPE_MISMATCH - if the encoded text exceeds field_len bytes

§Examples

use copybook_codec::numeric::encode_alphanumeric;
use copybook_codec::Codepage;

// Encode a 5-character string into a 10-byte ASCII field (right-padded with spaces)
let result = encode_alphanumeric("HELLO", 10, Codepage::ASCII).unwrap();
assert_eq!(result.len(), 10);
assert_eq!(&result[..5], b"HELLO");
assert_eq!(&result[5..], b"     "); // padded with spaces

§See Also