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