7 #ifndef RETDEC_UTILS_CONVERSION_H
8 #define RETDEC_UTILS_CONVERSION_H
15 #include <type_traits>
41 std::size_t offset = 0,
43 bool uppercase =
true,
46 if (data ==
nullptr || offset >= dataSize)
51 size = (size == 0 || offset + size > dataSize)
55 std::size_t hexIndex = 0;
57 std::size_t sz = spacing ? (size * 3 - 1) : (size * 2);
60 for (std::size_t i = 0; i < size; ++i)
62 if (spacing && hexIndex > 0)
64 result[hexIndex++] =
' ';
67 result[hexIndex++] = res[0];
68 result[hexIndex++] = res[1];
83 const std::vector<N> &bytes,
85 std::size_t offset = 0,
87 bool uppercase =
true,
114 static const char* digits =
"0123456789abcdef";
116 size_t hex_len =
sizeof(I)<<1;
118 std::string rc(hex_len,
'0');
119 for (
size_t i = 0, j = (hex_len-1)*4 ; i < hex_len; ++i, j -= 4)
121 rc[i] = digits[(w>>j) & 0x0f];
124 bool started =
false;
129 res.resize(rc.size() + 2);
136 res.resize(rc.size());
138 for (
size_t i = 0; i < rc.size(); ++i)
144 else if (rc[i] !=
'0' || (rc.size() - i <= fillToN) || (i == rc.size() - 1))
169 inline bool strToNum(
const std::string &str, N &number,
170 std::ios_base &(* format)(std::ios_base &) = std::dec) {
171 std::istringstream strStream(str);
173 strStream >> format >> convNumber;
174 if (strStream.fail() || !strStream.eof()) {
180 if (std::is_unsigned<N>::value && str[0] ==
'-') {
190 const std::size_t BITS_IN_BYTE = 8;
208 result.reserve(dataSize * BITS_IN_BYTE);
210 for (std::size_t i = 0; i < dataSize; ++i) {
211 auto& item = data[i];
213 for(std::size_t j = 0; j < BITS_IN_BYTE; ++j) {
215 result += ((item << j) & 0x80) ?
'1' :
'0';
244 std::size_t dataSize,
246 std::size_t offset = 0,
247 std::size_t size = 0)
254 if(offset >= dataSize)
260 size = (size == 0 || offset + size > dataSize)
266 result.reserve(size);
267 result = std::string(
reinterpret_cast<const char*
>(data + offset), size);
279 const std::vector<N> &bytes,
281 std::size_t offset = 0,
282 std::size_t size = 0)
284 bytesToString(bytes.data(), bytes.size(), result, offset, size);
288 const std::vector<unsigned char> &src);
290 unsigned short byteSwap16(
unsigned short val);
292 std::string
byteSwap16(
const std::string &val);
293 std::string
byteSwap32(
const std::string &val);
void bytesToString(const N *data, std::size_t dataSize, std::string &result, std::size_t offset=0, std::size_t size=0)
Definition: conversion.h:242
bool strToNum(const std::string &str, N &number, std::ios_base &(*format)(std::ios_base &)=std::dec)
Converts the given string into a number.
Definition: conversion.h:169
std::vector< uint8_t > hexStringToBytes(const std::string &hexIn)
Definition: conversion.cpp:170
void double10ToDouble8(std::vector< unsigned char > &dest, const std::vector< unsigned char > &src)
Convert 80-bit (10-byte) long double binary data (byte array) into 64-bit (8-byte) double binary data...
Definition: conversion.cpp:72
char * byteToHexString(uint8_t b, bool uppercase=true)
Definition: conversion.cpp:19
std::string intToHexString(I w, bool addBase=false, unsigned fillToN=0)
Converts the given integer into its hexadecimal representation.
Definition: conversion.h:112
std::string bytesToBits(const N *data, std::size_t dataSize)
Converts the given array of numbers into a bits.
Definition: conversion.h:202
unsigned short byteSwap16(unsigned short val)
Swap bytes for Intel x86 16-bit little-endian immediate.
Definition: conversion.cpp:118
unsigned int byteSwap32(unsigned int val)
Swap bytes for Intel x86 32-bit little-endian immediate.
Definition: conversion.cpp:129
void bytesToHexString(const N *data, std::size_t dataSize, std::string &result, std::size_t offset=0, std::size_t size=0, bool uppercase=true, bool spacing=false)
Definition: conversion.h:37
Definition: archive_wrapper.h:19