-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlempel_ziv.cpp
41 lines (35 loc) · 1.01 KB
/
lempel_ziv.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
#include "lempel_ziv.h"
#include <cstdio>
/**
* This methods return a binary string of characters of length n for the
* number given.
*
* @param number The number to be codified in binary.
* @param n The length of the string it should return.
*/
std::string LempelZivEncoder::toBinaryString(unsigned int number, unsigned int n)
{
std::string new_string("");
for (unsigned int i = 0; i < n; i++)
{
if (number >> i & 0x01)
new_string.insert(0, "1");
else new_string.insert(0, "0");
}
printf("%d %s\n", number, new_string.c_str());
return new_string;
}
/**
* This method does the opposite to the method "toBinaryString". It
* parses a binary string and return the number it represents.
*
* @param bin_string The string to be parsed by the method.
*/
unsigned int LempelZivEncoder::binStringToInteger(std::string bin_string)
{
unsigned int num = 0;
unsigned int j = 0;
for (int i = bin_string.size() - 1; i >= 0; i--, j++)
if (bin_string[i] == '1') num = num | 0x01 << j;
return num;
}