From abcd818f2f48f6805ca1bfadf9d9694078e6a963 Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Fri, 5 Jun 2020 16:44:31 +0200 Subject: [PATCH] printer renamed to arrayprinter --- globalfunctions/arrayprinter.cpp | 61 ++++++++++++++++++++++++++++++++ globalfunctions/arrayprinter.h | 20 +++++++++++ 2 files changed, 81 insertions(+) create mode 100644 globalfunctions/arrayprinter.cpp create mode 100644 globalfunctions/arrayprinter.h diff --git a/globalfunctions/arrayprinter.cpp b/globalfunctions/arrayprinter.cpp new file mode 100644 index 00000000..e8fce56c --- /dev/null +++ b/globalfunctions/arrayprinter.cpp @@ -0,0 +1,61 @@ +#include +#include +#include + +void arrayprinter::print(const uint8_t *data, size_t size, OutputType type, + bool printInfo, size_t maxCharPerLine) { + if(printInfo) { + sif::info << "Printing data with size " << size << ": "; + } + sif::info << "["; + if(type == OutputType::HEX) { + arrayprinter::printHex(data, size, maxCharPerLine); + } + else if (type == OutputType::DEC) { + arrayprinter::printDec(data, size, maxCharPerLine); + } + else if(type == OutputType::BIN) { + arrayprinter::printBin(data, size); + } +} + +void arrayprinter::printHex(const uint8_t *data, size_t size, + size_t maxCharPerLine) { + sif::info << std::hex; + for(size_t i = 0; i < size; i++) { + sif::info << "0x" << static_cast(data[i]); + if(i < size - 1){ + sif::info << " , "; + if(i > 0 and i % maxCharPerLine == 0) { + sif::info << std::endl; + } + } + + } + sif::info << std::dec; + sif::info << "]" << std::endl; +} + +void arrayprinter::printDec(const uint8_t *data, size_t size, + size_t maxCharPerLine) { + sif::info << std::dec; + for(size_t i = 0; i < size; i++) { + sif::info << static_cast(data[i]); + if(i < size - 1){ + sif::info << " , "; + if(i > 0 and i % maxCharPerLine == 0) { + sif::info << std::endl; + } + } + } + sif::info << "]" << std::endl; +} + +void arrayprinter::printBin(const uint8_t *data, size_t size) { + sif::info << "\n" << std::flush; + for(size_t i = 0; i < size; i++) { + sif::info << "Byte " << i + 1 << ": 0b"<< + std::bitset<8>(data[i]) << ",\n" << std::flush; + } + sif::info << "]" << std::endl; +} diff --git a/globalfunctions/arrayprinter.h b/globalfunctions/arrayprinter.h new file mode 100644 index 00000000..e57d8e04 --- /dev/null +++ b/globalfunctions/arrayprinter.h @@ -0,0 +1,20 @@ +#ifndef FRAMEWORK_GLOBALFUNCTIONS_ARRAYPRINTER_H_ +#define FRAMEWORK_GLOBALFUNCTIONS_ARRAYPRINTER_H_ +#include +#include + +enum class OutputType { + DEC, + HEX, + BIN +}; + +namespace arrayprinter { +void print(const uint8_t* data, size_t size, OutputType type = OutputType::HEX, + bool printInfo = true, size_t maxCharPerLine = 12); +void printHex(const uint8_t* data, size_t size, size_t maxCharPerLine = 12); +void printDec(const uint8_t* data, size_t size, size_t maxCharPerLine = 12); +void printBin(const uint8_t* data, size_t size); +} + +#endif /* FRAMEWORK_GLOBALFUNCTIONS_ARRAYPRINTER_H_ */