tlx
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 <tb@panthema.net>
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 Bytes in Most-Significant-Bit First Order
17 
18 std::string bitdump_8_msb(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 
44 std::string bitdump_8_msb(const std::string& str) {
45  return bitdump_8_msb(str.data(), str.size());
46 }
47 
48 std::string bitdump_le8(const void* const data, size_t size) {
49  return bitdump_8_msb(data, size);
50 }
51 
52 std::string bitdump_le8(const std::string& str) {
53  return bitdump_8_msb(str);
54 }
55 
56 /******************************************************************************/
57 // Bitdump 8-bit Bytes in Least-Significant-Bit First Order
58 
59 std::string bitdump_8_lsb(const void* const data, size_t size) {
60  const unsigned char* const cdata =
61  static_cast<const unsigned char*>(data);
62 
63  std::string out;
64  out.resize(size * 9 - 1);
65 
66  std::string::iterator oi = out.begin();
67  for (const unsigned char* si = cdata; si != cdata + size; ++si) {
68  *(oi + 0) = '0' + ((*si >> 0) & 1);
69  *(oi + 1) = '0' + ((*si >> 1) & 1);
70  *(oi + 2) = '0' + ((*si >> 2) & 1);
71  *(oi + 3) = '0' + ((*si >> 3) & 1);
72  *(oi + 4) = '0' + ((*si >> 4) & 1);
73  *(oi + 5) = '0' + ((*si >> 5) & 1);
74  *(oi + 6) = '0' + ((*si >> 6) & 1);
75  *(oi + 7) = '0' + ((*si >> 7) & 1);
76  oi += 8;
77  if (si + 1 != cdata + size) {
78  *oi++ = ' ';
79  }
80  }
81 
82  return out;
83 }
84 
85 std::string bitdump_8_lsb(const std::string& str) {
86  return bitdump_8_lsb(str.data(), str.size());
87 }
88 
89 std::string bitdump_be8(const void* const data, size_t size) {
90  return bitdump_8_lsb(data, size);
91 }
92 
93 std::string bitdump_be8(const std::string& str) {
94  return bitdump_8_lsb(str);
95 }
96 
97 /******************************************************************************/
98 
99 } // namespace tlx
100 
101 /******************************************************************************/
std::string bitdump_le8(const void *const data, size_t size)
deprecated method: unclear naming and documentation.
Definition: bitdump.cpp:48
std::string bitdump_8_msb(const void *const data, size_t size)
Dump a (binary) string of 8-bit bytes as a sequence of &#39;0&#39; and &#39;1&#39; characters, with the most signific...
Definition: bitdump.cpp:18
std::string bitdump_8_lsb(const void *const data, size_t size)
Dump a (binary) string of 8-bit bytes as a sequence of &#39;0&#39; and &#39;1&#39; characters, with the least signifi...
Definition: bitdump.cpp:59
std::string bitdump_be8(const void *const data, size_t size)
deprecated method: unclear naming and documentation.
Definition: bitdump.cpp:89