#binary #string #bin #conversion #hex


Converts From Hex, UTF-8, Vector of Bytes, or a Byte Slice to a Binary String And Implements Many Useful Functions For Binary Strings

5 releases (3 breaking)

0.4.0 Jan 11, 2021
0.3.0 Jun 5, 2020
0.2.1 May 18, 2020
0.2.0 May 18, 2020
0.1.0 May 17, 2020

#627 in Encoding

Download history 18/week @ 2023-02-10 22/week @ 2023-02-17 4/week @ 2023-02-24 10/week @ 2023-03-03 12/week @ 2023-03-10 205/week @ 2023-03-17 482/week @ 2023-03-24 266/week @ 2023-03-31 172/week @ 2023-04-07 512/week @ 2023-04-14 180/week @ 2023-04-21 132/week @ 2023-04-28 154/week @ 2023-05-05 13/week @ 2023-05-12 29/week @ 2023-05-19 31/week @ 2023-05-26

236 downloads per month


245 lines

To-Binary: A Rust Crate For Conversion To A BinaryString

Crates.io Crates.io Build Status

A Rust Library For Conversion To new type BinaryString. BinaryString is a singled-fielded, tuple struct that holds a BinaryString or BinaryWhitespaceString and is of the type String.

It allows:

  • Asserting Whether a Given Input Is a Binary String
  • Asserting Number of Bits/Bytes
  • Counting of Bits and Bytes
  • Adding/Removing Whitespace Between Bytes

How To Use

Read Examples and Read The Documentation

Conversion To BinaryString

These examples show how you can convert from one type to the BinaryString type.

use to_binary::{BinaryString,BinaryError};

fn main(){
    // Hexadecimal
    let hex = BinaryString::from_hex("2879E653864EA6047FEBBBD9AE6DA8DA").unwrap();
    // String
    let x = BinaryString::from(String::from("Test"));
  	// str
  	let y = BinaryString::from("Hello World");
  	// Byte
  	let byte = BinaryString::from(118u8);
  	// Vector
  	let vector = vec![36,57,123,38,2];
  	let bin_vector = BinaryString::from(vector);

Adding or Removing Whitespace From A BinaryString

This example shows how you can add whitespace between bytes or remove whitespace from a BinaryString

use to_binary::{BinaryString,BinaryError};

fn test_whitespace() {
        // Conversion To `BinaryString` Struct From Hex
        let x: BinaryString = BinaryString::from_hex("FF8628AA").unwrap();

        // Creates New Struct With Spaces
        let with_spaces: BinaryString = x.add_spaces().unwrap();

        // Removes Spaces And Creates New Struct
        let removed_spaces: BinaryString = with_spaces.remove_spaces();

        // Prints Out Information
        println!("x: {}", x);
        println!("with_spaces : {}", with_spaces);
        println!("removed spaces: {}", removed_spaces);

        let is_true: bool = with_spaces.assert_binary_whitespace();

        // Asserts It Works And The Answer Without Spaces Is Same As Initial
        assert_eq!(is_true, true);
        assert_eq!(x, removed_spaces);

Using Binary Methods On BinaryString

There are many BinaryString Methods That Can Be Used. This is a few of them that may be useful.

fn main(){
  	// Conversion To `BinaryString` Struct From Hex
  	let x: BinaryString = BinaryString::from_hex("FF8628AA").unwrap();
  	// Checks Whether The Input Is Binary
    let check_if_binary: bool = x.assert_binary();

    // Assert Input Is Binary
    assert_eq!(check_if_binary, true);

    // Retrieve Sizes Of Binary Input (Bits and Bytes)
    let size_in_bits = x.bits().unwrap();
    let size_in_bytes = x.bytes().unwrap();

    // Verifies Sizes Of Binary Inputs
    let verify_bit_length: bool = x.assert_bit_length(size_in_bits);
    let verify_byte_length: bool = x.assert_byte_length(size_in_bytes);

    // Assert Sizes Are Correct
    assert_eq!(verify_bit_length, true);
    assert_eq!(verify_byte_length, true);


Licensed under either of

  • Apache License, Version 2.0

  • MIT license

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.