pub fn string_to_binary(bits: &str) -> Vec<u8> {
    let mut bytes = Vec::new();
    let mut current_byte: u8 = 0;

    let mut count: u8 = 0;
    for c in bits.chars() {
        current_byte <<= 1;
        if c == '1' {
            current_byte += 1;
        }
        count += 1;

        if count >= 8 {
            bytes.push(current_byte);
            current_byte = 0;
            count = 0;
        }
    }

    if count > 0 {
        while count < 8 {
            current_byte <<= 1;
            count += 1;
        }
        bytes.push(current_byte);
    }

    bytes
}

pub fn binary_to_string(bytes: Vec<u8>, len: u32) -> String {
    let mut binary = String::new();
    //bytes.reverse();
    let mut word = String::new();
    let mut n = 0;
    for b in &bytes {
        let current = *b;
        for i in 0..8 {
            if current >> (7 - i) & 1 == 1 {
                word.push('1');
            } else {
                word.push('0');
            }
        }
        binary = binary + &word;
        word = String::new();
        n += 1;
        if n >= len {
            break;
        }
    }

    binary
}
