| 424 | } |
| 425 | |
| 426 | size_t d2Data(byte* buf, double d, ByteOrder byteOrder) { |
| 427 | // This algorithm assumes that the internal representation of the double |
| 428 | // type is the 8-byte IEEE 754 binary64 format, which is common but not |
| 429 | // required by the C++ standard. |
| 430 | union { |
| 431 | uint64_t ull_; |
| 432 | double d_; |
| 433 | } u; |
| 434 | u.d_ = d; |
| 435 | uint64_t m = 0xff; |
| 436 | if (byteOrder == littleEndian) { |
| 437 | buf[0] = static_cast<byte>(u.ull_ & m); |
| 438 | buf[1] = static_cast<byte>((u.ull_ & (m << 8)) >> 8); |
| 439 | buf[2] = static_cast<byte>((u.ull_ & (m << 16)) >> 16); |
| 440 | buf[3] = static_cast<byte>((u.ull_ & (m << 24)) >> 24); |
| 441 | buf[4] = static_cast<byte>((u.ull_ & (m << 32)) >> 32); |
| 442 | buf[5] = static_cast<byte>((u.ull_ & (m << 40)) >> 40); |
| 443 | buf[6] = static_cast<byte>((u.ull_ & (m << 48)) >> 48); |
| 444 | buf[7] = static_cast<byte>((u.ull_ & (m << 56)) >> 56); |
| 445 | } else { |
| 446 | buf[0] = static_cast<byte>((u.ull_ & (m << 56)) >> 56); |
| 447 | buf[1] = static_cast<byte>((u.ull_ & (m << 48)) >> 48); |
| 448 | buf[2] = static_cast<byte>((u.ull_ & (m << 40)) >> 40); |
| 449 | buf[3] = static_cast<byte>((u.ull_ & (m << 32)) >> 32); |
| 450 | buf[4] = static_cast<byte>((u.ull_ & (m << 24)) >> 24); |
| 451 | buf[5] = static_cast<byte>((u.ull_ & (m << 16)) >> 16); |
| 452 | buf[6] = static_cast<byte>((u.ull_ & (m << 8)) >> 8); |
| 453 | buf[7] = static_cast<byte>(u.ull_ & m); |
| 454 | } |
| 455 | return 8; |
| 456 | } |
| 457 | |
| 458 | void hexdump(std::ostream& os, const byte* buf, size_t len, size_t offset) { |
| 459 | const std::string::size_type pos = 8 + 16 * 3 + 2; |