1 #ifndef SWISSARMYKNIFE_BYTES_UTILS_HPP 2 #define SWISSARMYKNIFE_BYTES_UTILS_HPP 4 #include <pre/detail/namespace_compatibility.hpp> 12 #include <boost/shared_ptr.hpp> 13 #include <boost/cstdint.hpp> 14 #include <boost/container/vector.hpp> 15 #include <boost/container/map.hpp> 16 #include <boost/assign.hpp> 17 #include <boost/foreach.hpp> 19 namespace pre {
namespace bytes {
36 inline std::string
to_hexstring(
const boost::uint8_t *bytes,
const size_t length) {
40 if (bytes == NULL || length <= 0)
41 return std::string(
"");
46 const char nibbleToChar[] = {
'0',
'1',
'2',
'3',
'4',
'5',
47 '6',
'7',
'8',
'9',
'a',
'b',
51 out.reserve(length*2);
54 ch = (bytes[i] & 0xF0);
58 out.push_back(nibbleToChar[ch]);
60 ch = (bytes[i] & 0x0F);
62 out.push_back(nibbleToChar[ch]);
78 if (bytes.size() == 0)
79 return std::string(
"");
84 const char nibbleToChar[] = {
'0',
'1',
'2',
'3',
'4',
'5',
85 '6',
'7',
'8',
'9',
'a',
'b',
89 out.reserve(bytes.size()*2);
90 while (i < bytes.size()) {
92 ch = (bytes[i] & 0xF0);
96 out.push_back(nibbleToChar[ch]);
98 ch = (bytes[i] & 0x0F);
100 out.push_back(nibbleToChar[ch]);
113 inline std::string
to_binstring(
const boost::container::vector<boost::uint8_t> &byteArray) {
114 std::stringstream bitsRepresentation;
115 const int shift = 8 *
sizeof( uint8_t ) - 1;
116 const uint8_t mask = 1 << shift;
118 BOOST_FOREACH(uint8_t byte, byteArray) {
120 for (
size_t i = 1; i <= shift + 1; i++ ) {
121 bitsRepresentation << ( byte & mask ?
'1' :
'0' );
126 return bitsRepresentation.str();
134 inline boost::uint8_t
from_hexchar(
char highNibbleChar,
char lowNibbleChar) {
135 const boost::container::map<char, boost::uint8_t> charToNibble = boost::assign::map_list_of(
'0', 0)(
'1', 1)(
'2', 2)
136 (
'3', 3)(
'4', 4)(
'5', 5)
137 (
'6', 6)(
'7', 7)(
'8', 8)
138 (
'9', 9)(
'a', 10)(
'b', 11)
139 (
'c', 12)(
'd', 13)(
'e', 14)
141 if (!isdigit(highNibbleChar)) {
142 highNibbleChar = tolower(highNibbleChar);
145 if (!isdigit(lowNibbleChar)) {
146 lowNibbleChar = tolower(lowNibbleChar);
149 if ( (charToNibble.find(highNibbleChar) != charToNibble.end()) && ( charToNibble.find(lowNibbleChar) != charToNibble.end() ) ) {
150 boost::uint8_t highNibble = charToNibble.find(highNibbleChar)->second;
151 boost::uint8_t lowNibble = charToNibble.find(lowNibbleChar)->second;
154 lowNibble |= highNibble;
167 inline boost::container::vector<boost::uint8_t>
from_hexstring(
const std::string &hexString) {
170 if ((nibblePaddedSize & 1) != 0) {
174 boost::container::vector<boost::uint8_t> result;
175 result.reserve(nibblePaddedSize);
177 for (std::string::const_iterator iter = hexString.begin(); iter != hexString.end(); iter +=
BYTE_NIBBLES) {
179 if ( (iter+1) != hexString.end() ) {
195 std::vector<char> bytes;
196 for (
unsigned int i = 0; i < hex.length(); i += 2) {
197 std::string byteString = hex.substr(i, 2);
198 char byte = (char) strtol(byteString.c_str(), NULL, 16);
199 bytes.push_back(byte);
201 std::string bb(bytes.begin(),bytes.end());
210 template<
typename intX_t>
211 inline intX_t
from_byteArray(
const boost::container::vector<boost::uint8_t> &byteArray) {
214 for (
size_t i = 0; i <
sizeof(intX_t); ++i) {
215 result |=
static_cast<intX_t
>(byteArray[i] << (CHAR_BIT * (
sizeof(intX_t) - (i+1))));
228 inline boost::shared_ptr<boost::container::vector<boost::uint8_t> >
load_bytearray(
const std::string binaryFilePath) {
229 boost::shared_ptr<boost::container::vector<boost::uint8_t> > bytearray;
230 std::ifstream in(binaryFilePath.data(), std::ios_base::in | std::ios::binary);
234 std::streampos lengthToRead = in.tellg();
237 bytearray.reset(
new boost::container::vector<boost::uint8_t>(lengthToRead, 0x0));
238 in.read(reinterpret_cast<char*>(&bytearray->at(0)), lengthToRead);
241 throw std::runtime_error(
"File " + binaryFilePath +
" not found.");
boost::container::vector< boost::uint8_t > from_hexstring(const std::string &hexString)
This function can be used to parse a string of hexadecimal number back into bits representation. Note that it runs from left to right, putting eventually the last nibble empty if the size of the input string isn't a multiple of 2.
Definition: utils.hpp:167
const size_t BYTE_NIBBLES
Constant telling how much Nibbles are in a byte.
Definition: utils.hpp:29
std::string to_hexstring(const boost::uint8_t *bytes, const size_t length)
Convers the given byte array of the given length to a corresponding hexadecimal number in a string...
Definition: utils.hpp:36
std::string to_binstring(const boost::container::vector< boost::uint8_t > &byteArray)
Convers the given byte array of the given length to a corresponding binary number in a string...
Definition: utils.hpp:113
boost::shared_ptr< boost::container::vector< boost::uint8_t > > load_bytearray(const std::string binaryFilePath)
This methods opens up a file and parses it's bytes into a bytearray.
Definition: utils.hpp:228
const size_t NIBBLE_BITS
Constant telling how much bits there is in a nibble.
Definition: utils.hpp:24
std::string buffer_from_hexstring(const std::string &hex)
Converts a string of hexadecimal number into a binary buffer of this valeu.
Definition: utils.hpp:194
boost::uint8_t from_hexchar(char highNibbleChar, char lowNibbleChar)
Converts the given hexChars to the byte corresponding.
Definition: utils.hpp:134
intX_t from_byteArray(const boost::container::vector< boost::uint8_t > &byteArray)
This function transforms any byteArray in the chosen (u)int*_t type.
Definition: utils.hpp:211