Thrill  0.1
bitdump.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/string/bitdump.cpp
3  *
4  * Part of tlx - http://panthema.net/tlx
5  *
6  * Copyright (C) 2019 Timo Bingmann <[email protected]>
7  *
8  * All rights reserved. Published under the Boost Software License, Version 1.0
9  ******************************************************************************/
10 
11 #include <tlx/string/bitdump.hpp>
12 
13 namespace tlx {
14 
15 /******************************************************************************/
16 // Bitdump 8-bit Little-Endian Methods
17 
18 std::string bitdump_le8(const void* const data, size_t size) {
19  const unsigned char* const cdata =
20  static_cast<const unsigned char*>(data);
21 
22  std::string out;
23  out.resize(size * 9 - 1);
24 
25  std::string::iterator oi = out.begin();
26  for (const unsigned char* si = cdata; si != cdata + size; ++si) {
27  *(oi + 0) = '0' + ((*si >> 7) & 1);
28  *(oi + 1) = '0' + ((*si >> 6) & 1);
29  *(oi + 2) = '0' + ((*si >> 5) & 1);
30  *(oi + 3) = '0' + ((*si >> 4) & 1);
31  *(oi + 4) = '0' + ((*si >> 3) & 1);
32  *(oi + 5) = '0' + ((*si >> 2) & 1);
33  *(oi + 6) = '0' + ((*si >> 1) & 1);
34  *(oi + 7) = '0' + ((*si >> 0) & 1);
35  oi += 8;
36  if (si + 1 != cdata + size) {
37  *oi++ = ' ';
38  }
39  }
40 
41  return out;
42 }
43 
45  return bitdump_le8(str.data(), str.size());
46 }
47 
48 /******************************************************************************/
49 // Bitdump 8-bit Big-Endian Methods
50 
51 std::string bitdump_be8(const void* const data, size_t size) {
52  const unsigned char* const cdata =
53  static_cast<const unsigned char*>(data);
54 
55  std::string out;
56  out.resize(size * 9 - 1);
57 
58  std::string::iterator oi = out.begin();
59  for (const unsigned char* si = cdata; si != cdata + size; ++si) {
60  *(oi + 0) = '0' + ((*si >> 0) & 1);
61  *(oi + 1) = '0' + ((*si >> 1) & 1);
62  *(oi + 2) = '0' + ((*si >> 2) & 1);
63  *(oi + 3) = '0' + ((*si >> 3) & 1);
64  *(oi + 4) = '0' + ((*si >> 4) & 1);
65  *(oi + 5) = '0' + ((*si >> 5) & 1);
66  *(oi + 6) = '0' + ((*si >> 6) & 1);
67  *(oi + 7) = '0' + ((*si >> 7) & 1);
68  oi += 8;
69  if (si + 1 != cdata + size) {
70  *oi++ = ' ';
71  }
72  }
73 
74  return out;
75 }
76 
78  return bitdump_be8(str.data(), str.size());
79 }
80 
81 /******************************************************************************/
82 
83 } // namespace tlx
84 
85 /******************************************************************************/
std::string bitdump_le8(const void *const data, size_t size)
Dump a (binary) string as a sequence of 8-bit little-endian bytes.
Definition: bitdump.cpp:18
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220
std::string bitdump_be8(const void *const data, size_t size)
Dump a (binary) string as a sequence of 8-bit big-endian bytes.
Definition: bitdump.cpp:51