[fnd] Add SImpleTextOutput::stringToArray()

This commit is contained in:
jakcron 2018-08-20 19:25:10 +08:00
parent 12ad762666
commit c026aeee92
2 changed files with 30 additions and 0 deletions

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <string> #include <string>
#include <fnd/types.h> #include <fnd/types.h>
#include <fnd/Vec.h>
namespace fnd 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, size_t row_len, size_t indent_len);
static void hexDump(const byte_t* data, size_t 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 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<byte_t>& array);
private: private:
static const size_t kDefaultRowLen = 0x10; static const size_t kDefaultRowLen = 0x10;
static const size_t kDefaultByteGroupingSize = 1; static const size_t kDefaultByteGroupingSize = 1;

View file

@ -87,4 +87,31 @@ std::string fnd::SimpleTextOutput::arrayToString(const byte_t* data, size_t len,
ss << separator; ss << separator;
} }
return ss.str(); 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<byte_t>& 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]);
}
} }