Skip to main content

decode_binary_int

Function decode_binary_int 

Source
pub fn decode_binary_int(data: &[u8], bits: u16, signed: bool) -> Result<i64>
Expand description

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

Decodes big-endian binary integer fields. Supports 16-bit, 32-bit, and 64-bit widths. All binary integers use big-endian byte order as per COBOL specification.

§Arguments

  • data - Raw byte data containing the binary integer
  • bits - Bit width of the field (16, 32, or 64)
  • signed - Whether the field is signed (true) or unsigned (false)

§Returns

The decoded integer value as i64

§Errors

Returns an error if the binary data is invalid or the field size is unsupported.

§Examples

§16-bit Signed Integer

use copybook_codec::numeric::{decode_binary_int};

// 16-bit signed: -12345 = [0xCF, 0xC7] (big-endian)
let data = [0xCF, 0xC7];
let result = decode_binary_int(&data, 16, true)?;
assert_eq!(result, -12345);

§16-bit Unsigned Integer

use copybook_codec::numeric::{decode_binary_int};

// 16-bit unsigned: 54321 = [0xD4, 0x31]
let data = [0xD4, 0x31];
let result = decode_binary_int(&data, 16, false)?;
assert_eq!(result, 54321);

§32-bit Signed Integer

use copybook_codec::numeric::{decode_binary_int};

// 32-bit signed: -987654321 = [0xC5, 0x7D, 0x3C, 0x21]
let data = [0xC5, 0x7D, 0x3C, 0x21];
let result = decode_binary_int(&data, 32, true)?;
assert_eq!(result, -987654321);

§64-bit Signed Integer

use copybook_codec::numeric::{decode_binary_int};

// 64-bit signed: 9223372036854775807 = [0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07]
let data = [0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07];
let result = decode_binary_int(&data, 64, true)?;
assert_eq!(result, 9223372036854775807);

§See Also