Subversion Repositories HomeAutomation

Rev

Rev 1322 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1314 runge 1
/*
2
 * convert.cpp
3
 *
4
 *  Created on: Aug 04, 2009
5
 *      Author: Mattias Runge
6
 */
7
 
8
#include "convert.h"
9
 
10
namespace atom {
11
namespace convert {
12
 
1326 runge 13
string bool2string(const bool b)
14
{
15
    return b ? "true" : "false";
16
}
17
 
1314 runge 18
bool string2bool(const string s)
19
{
20
    return (s == "true" || s == "1" || s == "yes");
21
}
22
 
23
int string2int(const string s)
24
{
25
    istringstream in(s);
26
    int i;
27
    in >> i;
28
    return i;
29
}
30
 
31
string int2string(int i)
32
{
33
    ostringstream out;
34
    out << i;
35
    return out.str();
36
}
37
 
38
unsigned int string2uint(const string s)
39
{
40
    istringstream in(s);
41
    unsigned int i;
42
    in >> i;
43
    return i;
44
}
45
 
46
string uint2string(unsigned int i)
47
{
48
    ostringstream out;
49
    out << i;
50
    return out.str();
51
}
52
 
53
float string2float(const string s)
54
{
55
    istringstream in(s);
56
    float f;
57
    in >> f;
58
    return f;
59
}
60
 
61
string float2string(float f)
62
{
63
    ostringstream out;
64
    out << f;
65
    return out.str();
66
}
67
 
68
string bytes2string(byte_list bytes)
69
{
70
    string str = "";
71
 
72
    for (unsigned int n = 0; n < bytes.size(); n++)
73
    {
74
        str += bytes[n];
75
    }
76
 
77
    return str;
78
}
79
 
80
unsigned int hex2uint(string hex)
81
{
82
    return bin2uint(hex2bin(hex));
83
}
84
 
85
unsigned int bin2uint(string bin)
86
{
87
    unsigned int num = 0;
88
 
89
    for (int n = bin.length() - 1; n >= 0; n--)
90
    {
1322 runge 91
        if (bin[n] == '1') num += (unsigned int)pow(2.0f, (int)(bin.length() - 1 - n));
1314 runge 92
    }
93
 
94
    return num;
95
}
96
 
97
 
98
float bin2float(string bin)
99
{
100
    float num = 0;
101
 
102
    if (bin.length() == 0) return 0.0f;
103
 
104
    bool isNegative = false;
105
    ;
106
 
107
    if (bin[0] == '1')
108
    {
109
        bin = bin_invert(bin);
110
        isNegative = true;
111
    }
112
 
113
    for (int n = bin.length() - 1; n > 0; n--)
114
    {
115
        if (bin[n] == '1') num += pow(2.0f, (int)(bin.length() - 1 - n));
116
    }
117
 
118
    num /= 64.0f;
119
 
120
    if (isNegative) num = -num;
121
 
122
    return num;
123
}
124
 
125
string bin2hex(string bin)
126
{
127
    string hex;
128
 
129
    while (bin.size() > 0)
130
    {
131
        string part;
132
 
133
        try
134
        {
135
            part = bin.substr(0, 4);
136
        }
137
        catch (std::out_of_range& e)
138
        {
139
            //cout << "DEBUG: bin2hex exception: " << e.what() << "\n";
140
            //cout << "DEBUG: A bin=" << bin << endl;
141
            continue;
142
        }
143
 
144
        bin.erase(0, part.size());
145
 
146
        while (part.size() < 4)
147
            part = "0" + part;
148
 
149
        if (part == "0000")
150
            hex += '0';
151
        else if (part == "0001")
152
            hex += '1';
153
        else if (part == "0010")
154
            hex += '2';
155
        else if (part == "0011")
156
            hex += '3';
157
        else if (part == "0100")
158
            hex += '4';
159
        else if (part == "0101")
160
            hex += '5';
161
        else if (part == "0110")
162
            hex += '6';
163
        else if (part == "0111")
164
            hex += '7';
165
        else if (part == "1000")
166
            hex += '8';
167
        else if (part == "1001")
168
            hex += '9';
169
        else if (part == "1010")
170
            hex += 'a';
171
        else if (part == "1011")
172
            hex += 'b';
173
        else if (part == "1100")
174
            hex += 'c';
175
        else if (part == "1101")
176
            hex += 'd';
177
        else if (part == "1110")
178
            hex += 'e';
179
        else if (part == "1111") hex += 'f';
180
    }
181
 
182
    return hex;
183
}
184
 
185
string float2bin(float num, unsigned int length)
186
{
187
    string bin;
188
 
189
    ///FIXME
190
 
191
    while (bin.size() < length)
192
        bin = "0" + bin;
193
 
194
    return bin;
195
}
196
 
197
string hex2bin(string hex)
198
{
199
    to_lower(hex);
200
 
201
    string bin;
202
 
203
    for (unsigned int n = 0; n < hex.length(); n++)
204
    {
205
        switch (hex[n])
206
        {
207
        case '0':
208
            bin += "0000";
209
            break;
210
        case '1':
211
            bin += "0001";
212
            break;
213
        case '2':
214
            bin += "0010";
215
            break;
216
        case '3':
217
            bin += "0011";
218
            break;
219
        case '4':
220
            bin += "0100";
221
            break;
222
        case '5':
223
            bin += "0101";
224
            break;
225
        case '6':
226
            bin += "0110";
227
            break;
228
        case '7':
229
            bin += "0111";
230
            break;
231
        case '8':
232
            bin += "1000";
233
            break;
234
        case '9':
235
            bin += "1001";
236
            break;
237
        case 'a':
238
            bin += "1010";
239
            break;
240
        case 'b':
241
            bin += "1011";
242
            break;
243
        case 'c':
244
            bin += "1100";
245
            break;
246
        case 'd':
247
            bin += "1101";
248
            break;
249
        case 'e':
250
            bin += "1110";
251
            break;
252
        case 'f':
253
            bin += "1111";
254
            break;
255
        }
256
    }
257
 
258
    return bin;
259
}
260
 
261
string uint2bin(unsigned int num, unsigned int length)
262
{
263
    string bin;
264
 
265
    while (num)
266
    {
267
        bin = char((num & 1) + '0') + bin;
268
        num >>= 1;
269
    }
270
 
271
    while (bin.size() < length)
272
        bin = "0" + bin;
273
 
274
    return bin;
275
}
276
 
277
string uint2hex(unsigned int num, unsigned int length)
278
{
279
    return bin2hex(uint2bin(num, length));
280
}
281
 
282
 
283
string bin_invert(string bin)
284
{
285
    string inverted;
286
 
287
    for (unsigned int n = 0; n < bin.length(); n++)
288
        inverted += (bin[n] == '1' ? '0' : '1');
289
 
290
    return inverted;
291
}
292
 
1322 runge 293
float LOG2of2 = log2f(2);
294
 
295
unsigned int stateCount2bitCount(unsigned int stateCount)
296
{
297
    // Special case for zero states
298
    if (stateCount == 0)
299
    {
300
        return 1;
301
    }
302
 
303
    return ceil(log2f(stateCount + 1) / LOG2of2);
1314 runge 304
}
1322 runge 305
 
306
 
1314 runge 307
}
1322 runge 308
}