4 |
|
// |
5 |
|
// $Id$ |
6 |
|
|
7 |
< |
#include <iostream> |
8 |
< |
#include <string> |
9 |
< |
#include <vector> |
7 |
> |
#include "Binary.hpp" |
8 |
|
|
9 |
< |
#include <foreach.hpp> |
9 |
> |
#ifdef _WIN32 |
10 |
> |
#pragma warning(disable:4267) |
11 |
> |
#endif |
12 |
|
|
13 |
< |
/*template <typename Type> |
14 |
< |
std::string binary(const Type& type) |
13 |
> |
Binary::Binary(const ext::String& string, bool signed_) : bytes(string.GetSize() / 8, 0) |
14 |
|
{ |
15 |
< |
std::ostringstream string; |
17 |
< |
|
18 |
< |
_rforu (index, 0, 8 * sizeof (type)) |
15 |
> |
if (string.IsEmpty()) |
16 |
|
{ |
17 |
< |
bool bit(1 & type >> index); |
17 |
> |
bytes.InsertLast(0); |
18 |
|
|
19 |
< |
string << bit; |
19 |
> |
return; |
20 |
|
} |
21 |
|
|
22 |
< |
return string.str(); |
26 |
< |
}*/ |
22 |
> |
size_t index(string.GetSize() % 8), offset(index); |
23 |
|
|
24 |
< |
class Binary |
29 |
< |
{ |
30 |
< |
private: |
31 |
< |
std::vector<char> bytes; |
32 |
< |
public: |
33 |
< |
Binary(const std::string& string); |
34 |
< |
template <typename Type> |
35 |
< |
Binary(const Type& type); |
36 |
< |
operator std::string() const; |
37 |
< |
}; |
24 |
> |
_rforeach (ext::Vector<byte_t>, byte, bytes) _rfor (byte_t, bit, 0, 8) *byte |= (string[index++] == '1') << bit; |
25 |
|
|
26 |
< |
Binary::Binary(const std::string& string) |
27 |
< |
{ |
28 |
< |
throw; |
42 |
< |
} |
26 |
> |
if (offset != 0) |
27 |
> |
{ |
28 |
> |
bytes.InsertLast(0); |
29 |
|
|
30 |
< |
template <typename Type> |
45 |
< |
Binary::Binary(const Type& type) : bytes(sizeof (type)) |
46 |
< |
{ |
47 |
< |
char* type_(reinterpret_cast<char*>(const_cast<Type*>(&type))); |
30 |
> |
_rfor (byte_t, bit, offset, 8) bytes.Last() |= (signed_ && string[0] == '1') << bit; |
31 |
|
|
32 |
< |
_mforeach (std::vector<char>, byte, bytes) *byte = *type_++; |
50 |
< |
} |
32 |
> |
index = 0; |
33 |
|
|
34 |
< |
Binary::operator std::string() const |
53 |
< |
{ |
54 |
< |
std::string string; |
55 |
< |
|
56 |
< |
_rforeach (std::vector<char>, byte, bytes) |
57 |
< |
{ |
58 |
< |
_rfor (char, bit, 0, 8) |
59 |
< |
{ |
60 |
< |
string += 1 & *byte >> bit ? '1' : '0'; |
61 |
< |
} |
34 |
> |
_rfor (byte_t, bit, 0, offset) bytes.Last() |= (string[index++] == '1') << bit; |
35 |
|
} |
63 |
– |
|
64 |
– |
return string; |
65 |
– |
} |
66 |
– |
|
67 |
– |
std::ostream& operator<<(std::ostream& out, const Binary& binary) |
68 |
– |
{ |
69 |
– |
return out << std::string(binary); |
36 |
|
} |
37 |
|
|
38 |
< |
int main(int argc, char* argv[]) |
38 |
> |
Binary::operator ext::String() const |
39 |
|
{ |
40 |
< |
std::string hello("Hello, World!"); |
40 |
> |
ext::String string; |
41 |
|
|
42 |
< |
_foreach (std::string, atom, hello) |
77 |
< |
{ |
78 |
< |
std::cout << Binary(*atom) << " = " << *atom << std::endl; |
79 |
< |
} |
80 |
< |
|
81 |
< |
_fori (index, -10, 11) |
82 |
< |
{ |
83 |
< |
std::cout << Binary(index) << " = " << index << std::endl; |
84 |
< |
} |
85 |
< |
|
86 |
< |
_foru (index, -10, 11) |
87 |
< |
{ |
88 |
< |
std::cout << Binary(index) << " = " << index << std::endl; |
89 |
< |
} |
42 |
> |
_rforeach (const ext::Vector<byte_t>, byte, bytes) _rfor (byte_t, bit, 0, 8) string.InsertLast(1 & *byte >> bit ? '1' : '0'); |
43 |
|
|
44 |
< |
for (float index(-1.0); index < 1.0; index += 0.1) |
92 |
< |
{ |
93 |
< |
std::cout << Binary(index) << " = " << index << std::endl; |
94 |
< |
} |
95 |
< |
|
96 |
< |
for (double index(-1.0); index < 1.0; index += 0.1) |
97 |
< |
{ |
98 |
< |
std::cout << Binary(index) << " = " << index << std::endl; |
99 |
< |
} |
100 |
< |
|
101 |
< |
return 0; |
44 |
> |
return string; |
45 |
|
} |