ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Represent/Binary.cpp
Revision: 370
Committed: 2004-12-21T22:04:06-08:00 (20 years, 6 months ago) by douglas
File size: 901 byte(s)
Log Message:
Menes!

File Contents

# Content
1 // Represent
2 //
3 // Douglas Thrift
4 //
5 // $Id$
6
7 #include "Binary.hpp"
8
9 Binary::Binary(const ext::String& string, bool signed_) : bytes(string.GetSize() / 8, 0)
10 {
11 if (string.IsEmpty())
12 {
13 bytes.InsertLast(0);
14
15 return;
16 }
17 // string.insert(0, 8 - off, signed_ && string[0] == '1' ? '1' : '0');
18
19 size_t index(string.GetSize() % 8), offset(index);
20
21 _rmforeach (ext::Vector<byte_t>, byte, bytes) _rfor (byte_t, bit, 0, 8) *byte |= (string[index++] == '1') << bit;
22
23 if (offset != 0)
24 {
25 bytes.InsertLast(0);
26
27 _rfor (byte_t, bit, offset, 8) bytes.Last() |= (signed_ && string[0] == '1') << bit;
28
29 index = 0;
30
31 _rfor (byte_t, bit, 0, offset) bytes.Last() |= (string[index++] == '1') << bit;
32 }
33 }
34
35 Binary::operator ext::String() const
36 {
37 ext::String string;
38
39 _rforeach (ext::Vector<byte_t>, byte, bytes) _rfor (byte_t, bit, 0, 8) string.InsertLast(1 & *byte >> bit ? '1' : '0');
40
41 return string;
42 }

Properties

Name Value
svn:eol-style native
svn:keywords Id