Crate bitlab [−] [src]
Objective:
To extract a range of bits from a binary data source
Status
Experimental
Documentation
This crate is published at crates.io. The detailed documentation is available at docs.rs/bitlab
Version
0.4.2
Usage
- In your Cargo.toml file, add
bitlab = "0.4"
under[dependencies]
- In your source file, add
extern crate bitlab
anduse bitlab::*;
Example 1:
Start at bit offset 1, extract 3 bits and interpret the result as u8
use bitlab::*; let a: i8 = -33; // = 0b1101_1111; let b = a.get_u8(1, 3).unwrap(); // 1 --> 101 <-- 1111 // = 5 assert_eq!(b, 5);
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
use bitlab::*; let v: Vec<u8> = vec!{ 0x48, 0x61, 0x6C, 0x6C, 0x6F }; // = "Hallo" let bar = v.get_u16(1, 7, 3); // relevant bytes = 0x616C = 0b0110_000 --> 1_01 <-- 10_1100 // = 5 assert_eq!(bar.unwrap(), 5);
Example 3:
There is a very simple application in the examples directory, which extracts the color resolution from a real gif file. To run it enter the following in the command line
cargo run --release --example gif
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 a.get_u8(5,3) function extracts the bits 5,6 and 7 of the variable a and returns the result as a u8 variable |
ExtractBitsFromVecU8 |
Defines a number of functions, which extract a range of bits from a Vec |