1 |
douglas |
362 |
// Represent |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
|
|
#include <iostream> |
8 |
|
|
#include <string> |
9 |
|
|
#include <vector> |
10 |
|
|
|
11 |
|
|
#include <foreach.hpp> |
12 |
|
|
|
13 |
|
|
class Binary |
14 |
|
|
{ |
15 |
|
|
private: |
16 |
|
|
std::vector<char> bytes; |
17 |
|
|
public: |
18 |
douglas |
365 |
Binary(std::string& string, bool signed_ = false); |
19 |
douglas |
362 |
template <typename Type> |
20 |
|
|
Binary(const Type& type); |
21 |
douglas |
365 |
template <typename Type> |
22 |
|
|
Type convert(bool signed_ = false); |
23 |
douglas |
362 |
operator std::string() const; |
24 |
|
|
}; |
25 |
|
|
|
26 |
douglas |
365 |
Binary::Binary(std::string& string, bool signed_) : bytes(string.size() / 8, 0) |
27 |
douglas |
362 |
{ |
28 |
douglas |
365 |
std::string::size_type off(string.size() % 8); |
29 |
|
|
|
30 |
|
|
if (off != 0) |
31 |
|
|
{ |
32 |
|
|
bytes.push_back(0); |
33 |
|
|
string.insert(0, 8 - off, signed_ && string[0] == '1' ? '1' : '0'); |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
std::string::size_type index(0); |
37 |
|
|
|
38 |
douglas |
366 |
_rmforeach (std::vector<char>, byte, bytes) _rfor (char, bit, 0, 8) *byte |= (string[index++] == '1') << bit; |
39 |
douglas |
362 |
} |
40 |
|
|
|
41 |
|
|
template <typename Type> |
42 |
|
|
Binary::Binary(const Type& type) : bytes(sizeof (type)) |
43 |
|
|
{ |
44 |
|
|
char* type_(reinterpret_cast<char*>(const_cast<Type*>(&type))); |
45 |
|
|
|
46 |
|
|
_mforeach (std::vector<char>, byte, bytes) *byte = *type_++; |
47 |
|
|
} |
48 |
|
|
|
49 |
douglas |
365 |
template <typename Type> |
50 |
|
|
Type Binary::convert(bool signed_) |
51 |
|
|
{ |
52 |
|
|
Type type; |
53 |
|
|
|
54 |
|
|
if (sizeof (type) != bytes.size()) bytes.resize(sizeof (type), signed_ && bytes.back() >> 7 ? ~0 : 0); |
55 |
|
|
|
56 |
|
|
char* type_(reinterpret_cast<char*>(&type)); |
57 |
|
|
|
58 |
|
|
_foreach (std::vector<char>, byte, bytes) *type_++ = *byte; |
59 |
|
|
|
60 |
|
|
return type; |
61 |
|
|
} |
62 |
|
|
|
63 |
douglas |
362 |
Binary::operator std::string() const |
64 |
|
|
{ |
65 |
|
|
std::string string; |
66 |
|
|
|
67 |
douglas |
365 |
_rforeach (std::vector<char>, byte, bytes) _rfor (char, bit, 0, 8) string +=1 & *byte >> bit ? '1' : '0'; |
68 |
douglas |
362 |
|
69 |
|
|
return string; |
70 |
|
|
} |
71 |
|
|
|
72 |
douglas |
365 |
inline std::ostream& operator<<(std::ostream& out, const Binary& binary) |
73 |
douglas |
362 |
{ |
74 |
|
|
return out << std::string(binary); |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
int main(int argc, char* argv[]) |
78 |
|
|
{ |
79 |
|
|
std::string hello("Hello, World!"); |
80 |
|
|
|
81 |
|
|
_foreach (std::string, atom, hello) |
82 |
|
|
{ |
83 |
|
|
std::cout << Binary(*atom) << " = " << *atom << std::endl; |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
_fori (index, -10, 11) |
87 |
|
|
{ |
88 |
|
|
std::cout << Binary(index) << " = " << index << std::endl; |
89 |
|
|
} |
90 |
|
|
|
91 |
douglas |
365 |
_foru (index, -10, 11) |
92 |
douglas |
362 |
{ |
93 |
|
|
std::cout << Binary(index) << " = " << index << std::endl; |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
for (float index(-1.0); index < 1.0; index += 0.1) |
97 |
|
|
{ |
98 |
|
|
std::cout << Binary(index) << " = " << index << std::endl; |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
for (double index(-1.0); index < 1.0; index += 0.1) |
102 |
|
|
{ |
103 |
|
|
std::cout << Binary(index) << " = " << index << std::endl; |
104 |
|
|
} |
105 |
|
|
|
106 |
douglas |
365 |
std::string string("101001101001"); |
107 |
|
|
Binary binary(string, true); |
108 |
|
|
|
109 |
|
|
std::cout << binary << " = " << string << std::endl; |
110 |
|
|
|
111 |
|
|
float value(binary.convert<float>()); |
112 |
|
|
|
113 |
|
|
std::cout << binary << " = " << value << std::endl << Binary(value) << " = " << value << std::endl; |
114 |
|
|
|
115 |
douglas |
362 |
return 0; |
116 |
|
|
} |