-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUtils.cpp
64 lines (58 loc) · 2.18 KB
/
Utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* This file is a part of EAS Encoder.
*
* Copyright (C) 2018 Matthew Burket
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*/
#include <string>
#include <vector>
#include <bitset>
#include <stdexcept>
#include <iostream>
#include <typeinfo>
#include <sstream>
#include "Utils.h"
#include "audio.h"
/// Given a string convert each character to binary add that binary to the given vector
/// \param vector the vector to add the bits to
/// \param message the message to convert to binary
void Utils::string_to_bit_stream(std::vector<bool> &vector, std::string message) {
for (std::size_t i = 0; i < message.size(); i++) {
auto bitstring = std::bitset<8>(message.c_str()[i]);
std::stringstream input;
input << bitstring;
Utils::bit_string_to_bit_stream(vector, input.str());
}
}
/// Converts a string of "0" and "1" to bool values add them to the given vector
/// \param vector vector the vector to add the bits to
/// \param bitstring the bitstring to convert
void Utils::bit_string_to_bit_stream(std::vector<bool> &vector, std::string bitstring) {
for (char i : bitstring) {
if (i == '0') {
vector.push_back(false);
} else if (i == '1') {
vector.push_back(true);
}
else {
throw std::invalid_argument("The string can only contain 0 and 1");
}
}
}
/// https://stackoverflow.com/a/26343947
/// \param num number to convert
/// \param totalLength total length at the end
/// \return
std::string Utils::zero_pad_int(int num, int totalLength) {
return zero_pad_int(std::to_string(num), totalLength);
}
/// Given string will pre-append the number of zeros needed expand to the total length
/// \param old_string old string to pre-append
/// \param totalLength the derised end Length
/// \return a zero padded string of length totalLength
std::string Utils::zero_pad_int(const std::string &old_string, int totalLength) {
return std::string(totalLength - old_string.length(), '0') + old_string;
}