From c026aeee92987ce308956243efc4ccfc7336f7df Mon Sep 17 00:00:00 2001 From: jakcron Date: Mon, 20 Aug 2018 19:25:10 +0800 Subject: [PATCH] [fnd] Add SImpleTextOutput::stringToArray() --- lib/libfnd/include/fnd/SimpleTextOutput.h | 3 +++ lib/libfnd/source/SimpleTextOutput.cpp | 27 +++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/libfnd/include/fnd/SimpleTextOutput.h b/lib/libfnd/include/fnd/SimpleTextOutput.h index ef0516f..15c3c8e 100644 --- a/lib/libfnd/include/fnd/SimpleTextOutput.h +++ b/lib/libfnd/include/fnd/SimpleTextOutput.h @@ -1,6 +1,7 @@ #pragma once #include #include +#include namespace fnd { @@ -12,6 +13,8 @@ namespace fnd static void hexDump(const byte_t* data, size_t len, size_t row_len, size_t indent_len); static void hexDump(const byte_t* data, size_t len); static std::string arrayToString(const byte_t* data, size_t len, bool upper_case, const std::string& separator); + static void stringToArray(const std::string& str, fnd::Vec& array); + private: static const size_t kDefaultRowLen = 0x10; static const size_t kDefaultByteGroupingSize = 1; diff --git a/lib/libfnd/source/SimpleTextOutput.cpp b/lib/libfnd/source/SimpleTextOutput.cpp index c7f1582..1ee71ce 100644 --- a/lib/libfnd/source/SimpleTextOutput.cpp +++ b/lib/libfnd/source/SimpleTextOutput.cpp @@ -87,4 +87,31 @@ std::string fnd::SimpleTextOutput::arrayToString(const byte_t* data, size_t len, ss << separator; } return ss.str(); +} + +inline byte_t charToByte(char chr) +{ + if (chr >= 'a' && chr <= 'f') + return (chr - 'a') + 0xa; + else if (chr >= 'A' && chr <= 'F') + return (chr - 'A') + 0xa; + else if (chr >= '0' && chr <= '9') + return chr - '0'; + return 0; +} + +void fnd::SimpleTextOutput::stringToArray(const std::string& str, fnd::Vec& array) +{ + size_t size = str.size(); + if ((size % 2)) + { + return; + } + + array.alloc(size/2); + + for (size_t i = 0; i < array.size(); i++) + { + array[i] = (charToByte(str[i * 2]) << 4) | charToByte(str[(i * 2) + 1]); + } } \ No newline at end of file