converter.hh
Go to the documentation of this file.
#pragma once
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdint>
#include <cctype>
#include <climits> // LONG_MAX, LLONG_MAX
#include <ctime> // time_t, struct tm, difftime, time, mktime
class converter {
static converter * instance;
converter() {};
~converter() {
if (instance != NULL) {
delete instance;
instance = NULL;
}
};
public:
inline static converter & get_instance() {
if (instance == NULL) instance = new converter();
return *instance;
};
public:
typedef enum {
big_endian,
little_endian
} endian_t;
public:
inline uint8_t bcd_to_bin(const uint8_t p_value) {
return ((p_value / 16 * 10) + (p_value % 16));
};
inline uint8_t bin_to_bcd(const uint8_t p_value) {
return ((p_value / 10 * 16) + (p_value % 10));
};
uint16_t swap(const uint16_t p_value);
inline int16_t swap(const int16_t p_value) {
return static_cast<short>(swap(static_cast<uint16_t>(p_value)));
};
uint32_t swap(const uint32_t p_value);
inline int32_t swap(const int32_t p_value) {
return static_cast<int>(swap(static_cast<uint32_t>(p_value)));
};
std::string string_to_hexa(const std::string & p_value);
std::string bytes_to_hexa(const std::vector<uint8_t> & p_value);
std::vector<uint8_t> hexa_to_bytes(const std::string & p_value);
std::string time_to_string(const time_t p_time);
std::string time_to_string(const struct tm & p_time);
inline std::vector<uint8_t> short_to_bytes(const int16_t p_value, const endian_t p_endianess = big_endian) const {
std::vector<uint8_t> result(sizeof(short), 0x00);
for (int i = sizeof(short) - 1; i >= 0; i--) {
int offset = (sizeof(short) - 1 - i) * 8;
result[i] = static_cast<uint8_t>((p_value >> offset) & 0xFF);
} // End of 'for' statement
return result;
}; // End of short_to_bytes
inline int16_t bytes_to_short(const std::vector<uint8_t> & p_value, const endian_t p_endianess = big_endian) const {
// Sanity check
if (p_value.size() > sizeof(short)) {
return SHRT_MAX;
}
int16_t value = 0;
for (size_t i = 0; i < p_value.size(); i++) {
value = (value << 8) + (p_value[i] & 0xff);
} // End of 'for' statement
return value;
}; // End of bytes_to_short
inline std::vector<uint8_t> int_to_bytes(const int32_t p_value, const endian_t p_endianess = big_endian) const {
/*uint8_t bytes[sizeof(p_value)];
std::copy(
static_cast<const uint8_t *>(static_cast<const void *>(&p_value)),
static_cast<const uint8_t *>(static_cast<const void *>(&p_value)) + sizeof(p_value),
bytes
);
std::vector<uint8_t> result(bytes, bytes + sizeof(bytes) / sizeof(uint8_t));*/
std::vector<uint8_t> result(sizeof(int), 0x00);
for (int i = sizeof(int) - 1; i >= 0; i--) {
int offset = (sizeof(int) - 1 - i) * 8;
result[i] = static_cast<uint8_t>((p_value >> offset) & 0xFF);
} // End of 'for' statement
return result;
}; // End of int_to_bytes
inline int32_t bytes_to_int(const std::vector<uint8_t> & p_value, const endian_t p_endianess = big_endian) const {
// Sanity check
if (p_value.size() > sizeof(int)) {
return INT_MAX;
}
int32_t value = 0;
for (size_t i = 0; i < p_value.size(); i++) {
value = (value << 8) + (p_value[i] & 0xff);
} // End of 'for' statement
return value;
// return *((int *)(&p_value[0]));
}; // End of bytes_to_int
inline std::vector<uint8_t> long_to_bytes(const int64_t p_value, const endian_t p_endianess = big_endian) const {
/*uint8_t bytes[sizeof(p_value)];
std::copy(
static_cast<const uint8_t *>(static_cast<const void *>(&p_value)),
static_cast<const uint8_t *>(static_cast<const void *>(&p_value)) + sizeof(p_value),
bytes
);
std::vector<uint8_t> result(bytes, bytes + sizeof(bytes) / sizeof(uint8_t));*/
std::vector<uint8_t> result(sizeof(int64_t), 0x00);
for (int i = sizeof(int64_t) - 1; i >= 0; i--) {
int offset = (sizeof(int64_t) - 1 - i) * 8;
result[i] = static_cast<uint8_t>((p_value >> offset) & 0xFF);
} // End of 'for' statement
return result;
}; // End of long_to_bytes
inline int64_t bytes_to_long(const std::vector<uint8_t> & p_value, const endian_t p_endianess = big_endian) const {
// Sanity check
if (p_value.size() > sizeof(int64_t)) {
return LLONG_MAX;
}
int64_t value = 0;
for (size_t i = 0; i < p_value.size(); i++) {
value = (value << 8) + (p_value[i] & 0xff);
} // End of 'for' statement
return value;
// return *((long *)(&p_value[0]));
}; // End of bytes_to_long
inline std::vector<uint8_t> float_to_bytes(const float p_value) const {
uint8_t bytes[sizeof(p_value)];
std::copy(
static_cast<const uint8_t *>(static_cast<const void *>(&p_value)),
static_cast<const uint8_t *>(static_cast<const void *>(&p_value)) + sizeof(p_value),
bytes
);
std::vector<uint8_t> result(bytes, bytes + sizeof(bytes) / sizeof(uint8_t));
return result;
}; // End of float_to_long
inline float bytes_to_float(const std::vector<uint8_t> & p_value) const {
return *((float *)(&p_value[0]));
}; // End of bytes_to_float
inline std::vector<uint8_t> string_to_bytes(const std::string & p_value) const {
return std::vector<uint8_t>(p_value.begin(), p_value.end());
}; // End of string_to_bytes
inline std::string bytes_to_string(const std::vector<uint8_t> & p_value) const {
return std::string(p_value.begin(), p_value.end());
}; // End of bytes_to_string
public:
inline int32_t string_to_int(const std::string & p_value) const {
return std::stoi(p_value);
//return atoi(p_value.c_str());
}; // End of string_to_int
inline std::string int_to_string(const int32_t & p_value) const {
std::ostringstream ss;
ss << p_value;
return ss.str();
}; // End of string_to_bytes
public:
std::string trim(const std::string& p_value, const std::string& p_trim_chars = " \t");
std::vector<std::string> split_arguments_line(const std::string & p_value);
}; // End of class converter
converter::bytes_to_int
int32_t bytes_to_int(const std::vector< uint8_t > &p_value, const endian_t p_endianess=big_endian) const
Convert a bytes array into a 32-bits integer (int32_t)
Definition: converter.hh:212
converter::bcd_to_bin
uint8_t bcd_to_bin(const uint8_t p_value)
Convert a Binary Coded Decimal value into a binary value.
Definition: converter.hh:78
converter::string_to_int
int32_t string_to_int(const std::string &p_value) const
Convert a string into an integer.
Definition: converter.hh:315
converter::swap
int16_t swap(const int16_t p_value)
Definition: converter.hh:99
converter::endian_t
endian_t
Endianess style.
Definition: converter.hh:66
converter::big_endian
Definition: converter.hh:67
converter::split_arguments_line
std::vector< std::string > split_arguments_line(const std::string &p_value)
Convert the provided string into a list of arguments.
Definition: converter.cc:115
converter::trim
std::string trim(const std::string &p_value, const std::string &p_trim_chars=" \)
Returns a copy of the string, with leading and trailing special characters omitted.
Definition: converter.cc:104
converter::bin_to_bcd
uint8_t bin_to_bcd(const uint8_t p_value)
Convert a binary value into a Binary Coded Decimal value.
Definition: converter.hh:88
converter::~converter
~converter()
Default private dtor.
Definition: converter.hh:45
converter::int_to_bytes
std::vector< uint8_t > int_to_bytes(const int32_t p_value, const endian_t p_endianess=big_endian) const
Convert a 32-bits integer (int32_t) into a bytes array.
Definition: converter.hh:190
converter::converter
converter()
Default private ctor.
Definition: converter.hh:41
converter::time_to_string
std::string time_to_string(const time_t p_time)
Convert a time in time_t format into a string formated according to RFC 822, 1036, 1123, 2822.
Definition: converter.cc:92
converter::hexa_to_bytes
std::vector< uint8_t > hexa_to_bytes(const std::string &p_value)
Convert an hexadecimal string into a bytes array.
Definition: converter.cc:54
converter::instance
static converter * instance
Unique static object reference of this class.
Definition: converter.hh:36
converter::string_to_bytes
std::vector< uint8_t > string_to_bytes(const std::string &p_value) const
Convert a string into a bytes array.
Definition: converter.hh:296
converter::bytes_to_string
std::string bytes_to_string(const std::vector< uint8_t > &p_value) const
Convert a bytes array into a string.
Definition: converter.hh:305
converter::little_endian
Definition: converter.hh:68
converter::bytes_to_short
int16_t bytes_to_short(const std::vector< uint8_t > &p_value, const endian_t p_endianess=big_endian) const
Convert a bytes array into a 16-bits integer (int16_t)
Definition: converter.hh:172
converter::float_to_bytes
std::vector< uint8_t > float_to_bytes(const float p_value) const
Convert a float value into a bytes array.
Definition: converter.hh:271
converter::bytes_to_long
int64_t bytes_to_long(const std::vector< uint8_t > &p_value, const endian_t p_endianess=big_endian) const
Convert a bytes array into a 64-bits integer (int64_t)
Definition: converter.hh:253
converter::bytes_to_float
float bytes_to_float(const std::vector< uint8_t > &p_value) const
Convert a bytes array into a float.
Definition: converter.hh:287
converter::get_instance
static converter & get_instance()
Public accessor to the single object reference.
Definition: converter.hh:56
converter::swap
int32_t swap(const int32_t p_value)
Definition: converter.hh:108
converter::swap
uint16_t swap(const uint16_t p_value)
Swap two bytes length value (e.g. 0xCAFE becomes 0xFECA)
Definition: converter.cc:5
converter::bytes_to_hexa
std::string bytes_to_hexa(const std::vector< uint8_t > &p_value)
Convert a bytes array int32_t an hexadecimal string.
Definition: converter.cc:39
converter::long_to_bytes
std::vector< uint8_t > long_to_bytes(const int64_t p_value, const endian_t p_endianess=big_endian) const
Convert a 64-bits integer (int64_t) into a bytes array.
Definition: converter.hh:231
converter
This class provide a set of methods for types conversions.
Definition: converter.hh:31
converter::int_to_string
std::string int_to_string(const int32_t &p_value) const
Convert an integer into a string.
Definition: converter.hh:325
converter::short_to_bytes
std::vector< uint8_t > short_to_bytes(const int16_t p_value, const endian_t p_endianess=big_endian) const
Convert a 16-bits integer (int16_t) into a bytes array.
Definition: converter.hh:157
converter::string_to_hexa
std::string string_to_hexa(const std::string &p_value)
Convert a string into an hexadecimal string.
Definition: converter.cc:15