Crate bitlab [] [src]

Extracting a range of bits from binary data

Objective:

To help developing applications, which extract bit level data from a binary source such as spacecraft telemetry

Status

Experimental

Version

0.1.4

Examples

Example 1:

To extract 3 bits starting at bit index 5 within a byte (0xFF) and interpret them as an unsigned integer

use bitlab::*;
let a = 0xFFu8;
let b = a.get_u8(5, 3).unwrap();
//assert_eq!(b, 7);

Example 2:

The data source is a vector of u8 types. We want to go to byte offset 1, bit offset 7 and starting from there extract 3 bits as an u16

let v: Vec<u8> = vec!{ 0x48, 0x61, 0x6C, 0x6C, 0x6F }; // = "Hallo"
let bar = bitlab::u16(&v, 1, 7, 3); // relevant bytes = 0x616C = 0b0110000  --> 101 <-- 101100
assert_eq!(bar.unwrap(), 5);

MIT Licence

Copyright <2017, Kağan Kayal>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Traits

ExtractBitsFromIntegralTypes

Defines a number of functions, which extract a range of bits from primitive numeric types (u8, u16, u32 and u64, i8, i16, i32 and i64) and return the result as one of the following types (u8, u16, u32 and u64, i8, i16, i32 and i64) E.g. the get_u8(5,3) function extracts the bits 5,6 and 7 of the variable a and returns the result as a u8 variable

Functions

u8

Extracts a range of bits from a Vec and returns a Result object containing an 8 bit unsigned integer.

u16

Extracts a range of bits from a Vec and returns a Result object containing a 16 bit unsigned integer.

u32

Extracts a range of bits from a Vec and returns a Result object containing a 32 bit unsigned integer.

u64

Extracts a range of bits from a Vec and returns a Result object containing a 64 bit unsigned integer.