Rev 1610 | Rev 1646 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1610 | Rev 1642 | ||
|---|---|---|---|
| Line 303... | Line 303... | ||
| 303 | } |
303 | } |
| 304 | 304 | ||
| 305 | return xml::Node::NodeList(); |
305 | return xml::Node::NodeList(); |
| 306 | } |
306 | } |
| 307 | 307 | ||
| 308 | std::string Protocol::DecodeInt( |
308 | std::string Protocol::DecodeInt(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
| 309 | { |
309 | { |
| 310 | unsigned long raw_bit_value = bitset.Read(start_bit, bit_length); |
310 | unsigned long raw_bit_value = bitset.Read(start_bit, bit_length); |
| 311 | int raw_value = 0; |
311 | int raw_value = 0; |
| 312 | 312 | ||
| 313 | memcpy(&raw_value, &raw_bit_value, sizeof(raw_value)); |
313 | memcpy(&raw_value, &raw_bit_value, sizeof(raw_value)); |
| 314 | 314 | ||
| 315 | return boost::lexical_cast<std::string>(raw_value); |
315 | return boost::lexical_cast<std::string>(raw_value); |
| 316 | } |
316 | } |
| 317 | 317 | ||
| 318 | void Protocol::EncodeInt( |
318 | void Protocol::EncodeInt(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
| 319 | { |
319 | { |
| 320 | unsigned long raw_bit_value = 0; |
320 | unsigned long raw_bit_value = 0; |
| 321 | int raw_value = boost::lexical_cast<int>(value); |
321 | int raw_value = boost::lexical_cast<int>(value); |
| 322 | 322 | ||
| 323 | memcpy(&raw_bit_value, &raw_value, sizeof(raw_value)); |
323 | memcpy(&raw_bit_value, &raw_value, sizeof(raw_value)); |
| 324 | 324 | ||
| 325 | bitset.Write(start_bit, bit_length, raw_bit_value); |
325 | bitset.Write(start_bit, bit_length, raw_bit_value); |
| 326 | } |
326 | } |
| 327 | 327 | ||
| 328 | std::string Protocol::DecodeUint( |
328 | std::string Protocol::DecodeUint(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
| 329 | { |
329 | { |
| 330 | unsigned long raw_bit_value = bitset.Read(start_bit, bit_length); |
330 | unsigned long raw_bit_value = bitset.Read(start_bit, bit_length); |
| 331 | 331 | ||
| 332 | //LOG.Debug("DecodeUint: raw_bit_value=" + boost::lexical_cast<std::string>(raw_bit_value)); |
332 | //LOG.Debug("DecodeUint: raw_bit_value=" + boost::lexical_cast<std::string>(raw_bit_value)); |
| 333 | 333 | ||
| 334 | return boost::lexical_cast<std::string>((unsigned int)raw_bit_value); |
334 | return boost::lexical_cast<std::string>((unsigned int)raw_bit_value); |
| 335 | } |
335 | } |
| 336 | 336 | ||
| 337 | void Protocol::EncodeUint( |
337 | void Protocol::EncodeUint(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
| 338 | { |
338 | { |
| 339 | unsigned long raw_bit_value = boost::lexical_cast<unsigned int>(value); |
339 | unsigned long raw_bit_value = boost::lexical_cast<unsigned int>(value); |
| 340 | 340 | ||
| 341 | bitset.Write(start_bit, bit_length, raw_bit_value); |
341 | bitset.Write(start_bit, bit_length, raw_bit_value); |
| 342 | } |
342 | } |
| 343 | 343 | ||
| 344 | std::string Protocol::DecodeFloat( |
344 | std::string Protocol::DecodeFloat(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
| 345 | { |
345 | { |
| 346 | float float_value = boost::lexical_cast<float>(this->DecodeInt(bitset, start_bit, bit_length)) / 64.0f; |
346 | float float_value = boost::lexical_cast<float>(this->DecodeInt(bitset, start_bit, bit_length)) / 64.0f; |
| 347 | 347 | ||
| 348 | return boost::lexical_cast<std::string>(float_value); |
348 | return boost::lexical_cast<std::string>(float_value); |
| 349 | } |
349 | } |
| 350 | 350 | ||
| 351 | void Protocol::EncodeFloat( |
351 | void Protocol::EncodeFloat(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
| 352 | { |
352 | { |
| 353 | int int_value = boost::lexical_cast<float>(value) * 64.0f; |
353 | int int_value = boost::lexical_cast<float>(value) * 64.0f; |
| 354 | 354 | ||
| 355 | this->EncodeInt(bitset, start_bit, bit_length, boost::lexical_cast<std::string>(int_value)); |
355 | this->EncodeInt(bitset, start_bit, bit_length, boost::lexical_cast<std::string>(int_value)); |
| 356 | } |
356 | } |
| 357 | 357 | ||
| 358 | std::string Protocol::DecodeAscii( |
358 | std::string Protocol::DecodeAscii(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
| 359 | { |
359 | { |
| 360 | std::string value; |
360 | std::string value; |
| 361 | 361 | ||
| 362 | for (unsigned int n = 0; n < bit_length; n += 8) |
362 | for (unsigned int n = 0; n < bit_length; n += 8) |
| 363 | { |
363 | { |
| Line 366... | Line 366... | ||
| 366 | } |
366 | } |
| 367 | 367 | ||
| 368 | return value; |
368 | return value; |
| 369 | } |
369 | } |
| 370 | 370 | ||
| 371 | void Protocol::EncodeAscii( |
371 | void Protocol::EncodeAscii(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
| 372 | { |
372 | { |
| 373 | //LOG.Debug("EncodeAscii: " + value + ", length=" + boost::lexical_cast<std::string>(value.length()) + ", bit_length=" + boost::lexical_cast<std::string>(bit_length)); |
373 | //LOG.Debug("EncodeAscii: " + value + ", length=" + boost::lexical_cast<std::string>(value.length()) + ", bit_length=" + boost::lexical_cast<std::string>(bit_length)); |
| 374 | 374 | ||
| 375 | for (unsigned int n = 0; n < bit_length; n += 8) |
375 | for (unsigned int n = 0; n < bit_length; n += 8) |
| 376 | { |
376 | { |
| Line 383... | Line 383... | ||
| 383 | 383 | ||
| 384 | bitset.Write(start_bit + n, 8, value[n / 8]); |
384 | bitset.Write(start_bit + n, 8, value[n / 8]); |
| 385 | } |
385 | } |
| 386 | } |
386 | } |
| 387 | 387 | ||
| 388 | std::string Protocol::DecodeHexstring( |
388 | std::string Protocol::DecodeHexstring(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
| 389 | { |
389 | { |
| 390 | std::string value; |
390 | std::string value; |
| 391 | char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; |
391 | char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; |
| 392 | 392 | ||
| 393 | for (unsigned int n = 0; n < bit_length; n += 4) |
393 | for (unsigned int n = 0; n < bit_length; n += 4) |
| Line 405... | Line 405... | ||
| 405 | } |
405 | } |
| 406 | 406 | ||
| 407 | return value; |
407 | return value; |
| 408 | } |
408 | } |
| 409 | 409 | ||
| 410 | void Protocol::EncodeHexstring( |
410 | void Protocol::EncodeHexstring(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
| 411 | { |
411 | { |
| 412 | int ascii_value; |
412 | int ascii_value; |
| 413 | 413 | ||
| 414 | for (unsigned int n = 0; n < bit_length; n += 4) |
414 | for (unsigned int n = 0; n < bit_length; n += 4) |
| 415 | { |
415 | { |