ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Represent/Binary.cpp
(Generate patch)

Comparing:
Represent/represent.cpp (file contents), Revision 363 by douglas, 2004-12-21T01:15:40-08:00 vs.
Represent/Binary.cpp (file contents), Revision 371 by douglas, 2004-12-21T23:19:54-08:00

# Line 4 | Line 4
4   //
5   // $Id$
6  
7 < #include <iostream>
8 < #include <string>
9 < #include <vector>
7 > #include "Binary.hpp"
8  
9 < #include <foreach.hpp>
12 <
13 < class Binary
14 < {
15 < private:
16 <        std::vector<char> bytes;
17 < public:
18 <        Binary(const std::string& string);
19 <        template <typename Type>
20 <        Binary(const Type& type);
21 <        operator std::string() const;
22 < };
23 <
24 < Binary::Binary(const std::string& string)
25 < {
26 <        throw;
27 < }
28 <
29 < template <typename Type>
30 < Binary::Binary(const Type& type) : bytes(sizeof (type))
9 > Binary::Binary(const ext::String& string, bool signed_) : bytes(string.GetSize() / 8, 0)
10   {
11 <        char* type_(reinterpret_cast<char*>(const_cast<Type*>(&type)));
33 <
34 <        _mforeach (std::vector<char>, byte, bytes) *byte = *type_++;
35 < }
36 <
37 < Binary::operator std::string() const
38 < {
39 <        std::string string;
40 <
41 <        _rforeach (std::vector<char>, byte, bytes)
11 >        if (string.IsEmpty())
12          {
13 <                _rfor (char, bit, 0, 8)
44 <                {
45 <                        string += 1 & *byte >> bit ? '1' : '0';
46 <                }
47 <        }
13 >                bytes.InsertLast(0);
14  
15 <        return string;
16 < }
15 >                return;
16 >        }
17  
18 < std::ostream& operator<<(std::ostream& out, const Binary& binary)
53 < {
54 <        return out << std::string(binary);
55 < }
18 >        size_t index(string.GetSize() % 8), offset(index);
19  
20 < int main(int argc, char* argv[])
58 < {
59 <        std::string hello("Hello, World!");
20 >        _rmforeach (ext::Vector<byte_t>, byte, bytes) _rfor (byte_t, bit, 0, 8) *byte |= (string[index++] == '1') << bit;
21  
22 <        _foreach (std::string, atom, hello)
22 >        if (offset != 0)
23          {
24 <                std::cout << Binary(*atom) << " = " << *atom << std::endl;
64 <        }
24 >                bytes.InsertLast(0);
25  
26 <        _fori (index, -10, 11)
67 <        {
68 <                std::cout << Binary(index) << " = " << index << std::endl;
69 <        }
26 >                _rfor (byte_t, bit, offset, 8) bytes.Last() |= (signed_ && string[0] == '1') << bit;
27  
28 <        _foru (index, 4294967286, 11)
72 <        {
73 <                std::cout << Binary(index) << " = " << index << std::endl;
74 <        }
28 >                index = 0;
29  
30 <        for (float index(-1.0); index < 1.0; index += 0.1)
77 <        {
78 <                std::cout << Binary(index) << " = " << index << std::endl;
30 >                _rfor (byte_t, bit, 0, offset) bytes.Last() |= (string[index++] == '1') << bit;
31          }
32 + }
33  
34 <        for (double index(-1.0); index < 1.0; index += 0.1)
35 <        {
36 <                std::cout << Binary(index) << " = " << index << std::endl;
37 <        }
34 > Binary::operator ext::String() const
35 > {
36 >        ext::String string;
37 >
38 >        _rforeach (ext::Vector<byte_t>, byte, bytes) _rfor (byte_t, bit, 0, 8) string.InsertLast(1 & *byte >> bit ? '1' : '0');
39  
40 <        return 0;
40 >        return string;
41   }

Comparing:
Represent/represent.cpp (property svn:eol-style), Revision 363 by douglas, 2004-12-21T01:15:40-08:00 vs.
Represent/Binary.cpp (property svn:eol-style), Revision 371 by douglas, 2004-12-21T23:19:54-08:00

# Line 0 | Line 1
1 + native

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines