Skip to main content

encode_binary_int

Function encode_binary_int 

Source
pub fn encode_binary_int(value: i64, bits: u16, signed: bool) -> Result<Vec<u8>>
Expand description

Encode binary integer field (COMP-4, COMP-5, BINARY)

Encodes integer values to big-endian binary format. Supports 16-bit, 32-bit, and 64-bit widths. All binary integers use big-endian byte order as per COBOL specification.

§Arguments

  • value - Integer value to encode
  • bits - Bit width of field (16, 32, or 64)
  • signed - Whether the field is signed (true) or unsigned (false)

§Returns

A vector of bytes containing the encoded binary integer

§Errors

Returns an error if the value is out of range for the specified bit width.

§Examples

§16-bit Signed Integer

use copybook_codec::numeric::{encode_binary_int};

// Encode -12345 as 16-bit signed
let encoded = encode_binary_int(-12345, 16, true)?;
assert_eq!(encoded, [0xCF, 0xC7]); // Big-endian

§16-bit Unsigned Integer

use copybook_codec::numeric::{encode_binary_int};

// Encode 54321 as 16-bit unsigned
let encoded = encode_binary_int(54321, 16, false)?;
assert_eq!(encoded, [0xD4, 0x31]);

§32-bit Signed Integer

use copybook_codec::numeric::{encode_binary_int};

// Encode -987654321 as 32-bit signed
let encoded = encode_binary_int(-987654321, 32, true)?;
assert_eq!(encoded, [0xC5, 0x7D, 0x3C, 0x21]);

§64-bit Signed Integer

use copybook_codec::numeric::{encode_binary_int};

// Encode 9223372036854775807 as 64-bit signed
let encoded = encode_binary_int(9223372036854775807, 64, true)?;
assert_eq!(encoded, [0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07]);

§See Also