ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Represent/Binary.cpp
Revision: 372
Committed: 2004-12-21T23:53:49-08:00 (20 years, 6 months ago) by douglas
File size: 881 byte(s)
Log Message:
Portable to Windows, eh?

File Contents

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

Properties

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