10 |
|
|
11 |
|
#include <foreach.hpp> |
12 |
|
|
13 |
– |
/*template <typename Type> |
14 |
– |
std::string binary(const Type& type) |
15 |
– |
{ |
16 |
– |
std::ostringstream string; |
17 |
– |
|
18 |
– |
_rforu (index, 0, 8 * sizeof (type)) |
19 |
– |
{ |
20 |
– |
bool bit(1 & type >> index); |
21 |
– |
|
22 |
– |
string << bit; |
23 |
– |
} |
24 |
– |
|
25 |
– |
return string.str(); |
26 |
– |
}*/ |
27 |
– |
|
13 |
|
class Binary |
14 |
|
{ |
15 |
|
private: |
16 |
|
std::vector<char> bytes; |
17 |
|
public: |
18 |
< |
Binary(const std::string& string); |
18 |
> |
Binary(std::string& string, bool signed_ = false); |
19 |
|
template <typename Type> |
20 |
|
Binary(const Type& type); |
21 |
+ |
template <typename Type> |
22 |
+ |
Type convert(bool signed_ = false); |
23 |
|
operator std::string() const; |
24 |
|
}; |
25 |
|
|
26 |
< |
Binary::Binary(const std::string& string) |
26 |
> |
Binary::Binary(std::string& string, bool signed_) : bytes(string.size() / 8, 0) |
27 |
|
{ |
28 |
< |
throw; |
28 |
> |
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 |
> |
_rmforeach (std::vector<char>, byte, bytes) |
39 |
> |
{ |
40 |
> |
_rfor (char, bit, 0, 8) |
41 |
> |
{ |
42 |
> |
*byte |= (string[index++] == '1') << bit; |
43 |
> |
} |
44 |
> |
} |
45 |
|
} |
46 |
|
|
47 |
|
template <typename Type> |
52 |
|
_mforeach (std::vector<char>, byte, bytes) *byte = *type_++; |
53 |
|
} |
54 |
|
|
55 |
+ |
template <typename Type> |
56 |
+ |
Type Binary::convert(bool signed_) |
57 |
+ |
{ |
58 |
+ |
Type type; |
59 |
+ |
|
60 |
+ |
if (sizeof (type) != bytes.size()) bytes.resize(sizeof (type), signed_ && bytes.back() >> 7 ? ~0 : 0); |
61 |
+ |
|
62 |
+ |
char* type_(reinterpret_cast<char*>(&type)); |
63 |
+ |
|
64 |
+ |
_foreach (std::vector<char>, byte, bytes) *type_++ = *byte; |
65 |
+ |
|
66 |
+ |
return type; |
67 |
+ |
} |
68 |
+ |
|
69 |
|
Binary::operator std::string() const |
70 |
|
{ |
71 |
|
std::string string; |
72 |
|
|
73 |
< |
_rforeach (std::vector<char>, byte, bytes) |
57 |
< |
{ |
58 |
< |
_rfor (char, bit, 0, 8) |
59 |
< |
{ |
60 |
< |
string += 1 & *byte >> bit ? '1' : '0'; |
61 |
< |
} |
62 |
< |
} |
73 |
> |
_rforeach (std::vector<char>, byte, bytes) _rfor (char, bit, 0, 8) string +=1 & *byte >> bit ? '1' : '0'; |
74 |
|
|
75 |
|
return string; |
76 |
|
} |
77 |
|
|
78 |
< |
std::ostream& operator<<(std::ostream& out, const Binary& binary) |
78 |
> |
inline std::ostream& operator<<(std::ostream& out, const Binary& binary) |
79 |
|
{ |
80 |
|
return out << std::string(binary); |
81 |
|
} |
109 |
|
std::cout << Binary(index) << " = " << index << std::endl; |
110 |
|
} |
111 |
|
|
112 |
+ |
std::string string("101001101001"); |
113 |
+ |
Binary binary(string, true); |
114 |
+ |
|
115 |
+ |
std::cout << binary << " = " << string << std::endl; |
116 |
+ |
|
117 |
+ |
float value(binary.convert<float>()); |
118 |
+ |
|
119 |
+ |
std::cout << binary << " = " << value << std::endl << Binary(value) << " = " << value << std::endl; |
120 |
+ |
|
121 |
|
return 0; |
122 |
|
} |