CbmRoot
Loading...
Searching...
No Matches
CbmMcbm2018UnpackerUtilRich2020.cxx
Go to the documentation of this file.
1/* Copyright (C) 2019 Justus-Liebig-Universitaet Giessen, Giessen
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Egor Ovcharenko [committer] */
4
6
7#include <iostream>
8
9std::string mRichSupport::GetBinaryRepresentation(size_t const size, uint8_t const* const ptr)
10{
11 std::string outString;
12
13 unsigned char* b = (unsigned char*) ptr;
14 unsigned char byte;
15
16 size_t buf_size = 2;
17 char cStr[buf_size];
18 cStr[1] = '\0';
19
20 for (int i = size - 1; i >= 0; i--) {
21 for (int j = 7; j >= 0; j--) {
22 byte = (b[i] >> j) & 1;
23 snprintf(cStr, buf_size, "%u", byte);
24 outString.append(cStr);
25 }
26 }
27
28 return outString;
29}
30
34std::string mRichSupport::GetHexRepresentation(size_t const size, uint8_t const* const ptr)
35{
36 std::string outString;
37
38 unsigned char* b = (unsigned char*) ptr;
39 unsigned char byte;
40
41 size_t buf_size = 3;
42 char cStr[buf_size];
43 cStr[2] = '\0';
44
45 for (int i = size - 1; i >= 0; i--) {
46 byte = b[i] & 0xff;
47 snprintf(cStr, buf_size, "%02x", byte);
48 outString.append(cStr);
49 }
50
51 return outString;
52}
53
54std::string mRichSupport::GetWordHexRepr(uint8_t const* const ptr)
55{
56 std::string outString;
57
58 unsigned char* b = (unsigned char*) ptr;
59 unsigned char byte[4];
60 byte[0] = b[3] & 0xff;
61 byte[1] = b[2] & 0xff;
62 byte[2] = b[1] & 0xff;
63 byte[3] = b[0] & 0xff;
64
65 size_t buf_size = 10;
66 char cStr[buf_size];
67 cStr[9] = '\0';
68
69 snprintf(cStr, buf_size, "%02x%02x %02x%02x", byte[0], byte[1], byte[2], byte[3]);
70
71 outString.append(cStr);
72
73 return outString;
74}
75
76std::string mRichSupport::GetWordHexReprInv(uint8_t const* const ptr)
77{
78 std::string outString;
79
80 unsigned char* b = (unsigned char*) ptr;
81 unsigned char byte[4];
82 byte[0] = b[0] & 0xff;
83 byte[1] = b[1] & 0xff;
84 byte[2] = b[2] & 0xff;
85 byte[3] = b[3] & 0xff;
86
87 size_t buf_size = 10;
88 char cStr[buf_size];
89 cStr[9] = '\0';
90
91 snprintf(cStr, buf_size, "%02x%02x %02x%02x", byte[0], byte[1], byte[2], byte[3]);
92
93 outString.append(cStr);
94
95 return outString;
96}
97
98void mRichSupport::SwapBytes(size_t const /*size*/, uint8_t const* ptr)
99{
100 unsigned char* b = (unsigned char*) ptr;
101 unsigned char byte[4];
102 byte[0] = b[3] & 0xff;
103 byte[1] = b[2] & 0xff;
104 byte[2] = b[1] & 0xff;
105 byte[3] = b[0] & 0xff;
106
107 b[0] = byte[0];
108 b[1] = byte[1];
109 b[2] = byte[2];
110 b[3] = byte[3];
111}
112
113void mRichSupport::PrintRaw(size_t const size, uint8_t const* const ptr)
114{
115 size_t nWords = size / 4;
116 // size_t nRestBytes = size%4;
117
118 for (size_t iWord = 0; iWord < nWords; iWord++) {
119 //std::cout << GetHexRepresentation(4, ptr+iWord*4) << " ";
120
121 std::cout << GetWordHexReprInv(ptr + iWord * 4) << " ";
122 }
123 /*if (nRestBytes > 0) {
124 std::cout << GetHexRepresentation(nRestBytes, ptr+nWords*4) << " " << std::endl;
125 } else {
126 std::cout << std::endl;
127 }*/
128 std::cout << std::endl;
129}
static constexpr size_t size()
Definition KfSimdPseudo.h:2
std::string GetHexRepresentation(size_t const size, uint8_t const *const ptr)
void PrintRaw(size_t const size, uint8_t const *const ptr)
std::string GetBinaryRepresentation(size_t const size, uint8_t const *const ptr)
std::string GetWordHexReprInv(uint8_t const *const ptr)
std::string GetWordHexRepr(uint8_t const *const ptr)
void SwapBytes(size_t const size, uint8_t const *ptr)