ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Represent/Binary.cpp
Revision: 371
Committed: 2004-12-21T23:19:54-08:00 (20 years, 6 months ago) by douglas
File size: 829 byte(s)
Log Message:
Sleepy.

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
18 size_t index(string.GetSize() % 8), offset(index);
19
20 _rmforeach (ext::Vector<byte_t>, byte, bytes) _rfor (byte_t, bit, 0, 8) *byte |= (string[index++] == '1') << bit;
21
22 if (offset != 0)
23 {
24 bytes.InsertLast(0);
25
26 _rfor (byte_t, bit, offset, 8) bytes.Last() |= (signed_ && string[0] == '1') << bit;
27
28 index = 0;
29
30 _rfor (byte_t, bit, 0, offset) bytes.Last() |= (string[index++] == '1') << bit;
31 }
32 }
33
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 string;
41 }

Properties

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