25#ifndef DCCLBINARY20100713H
26#define DCCLBINARY20100713H
35#include "thirdparty/base64/base64.hpp"
46inline void hex_decode(
const std::string& in, std::string* out)
48 static constexpr short char0_9_to_number = 48;
49 static constexpr short charA_F_to_number = 55;
50 static constexpr short chara_f_to_number = 87;
52 int in_size = in.size();
53 int out_size = in_size >> 1;
57 out->assign(out_size,
'\0');
58 for (
int i = (in_size & 1) ? -1 : 0, n = in_size; i < n; i += 2)
60 int out_i = (in_size & 1) ? (i + 1) / 2 : i / 2;
64 if (in[i] >=
'0' && in[i] <=
'9')
65 (*out)[out_i] |= ((in[i] - char0_9_to_number) & 0x0f) << 4;
66 else if (in[i] >=
'A' && in[i] <=
'F')
67 (*out)[out_i] |= ((in[i] - charA_F_to_number) & 0x0f) << 4;
68 else if (in[i] >=
'a' && in[i] <=
'f')
69 (*out)[out_i] |= ((in[i] - chara_f_to_number) & 0x0f) << 4;
72 if (in[i + 1] >=
'0' && in[i + 1] <=
'9')
73 (*out)[out_i] |= (in[i + 1] - char0_9_to_number) & 0x0f;
74 else if (in[i + 1] >=
'A' && in[i + 1] <=
'F')
75 (*out)[out_i] |= (in[i + 1] - charA_F_to_number) & 0x0f;
76 else if (in[i + 1] >=
'a' && in[i + 1] <=
'f')
77 (*out)[out_i] |= (in[i + 1] - chara_f_to_number) & 0x0f;
81inline std::string
hex_decode(
const std::string& in)
94template <
typename CharIterator>
95inline void hex_encode(CharIterator begin, CharIterator end, std::string* out,
96 bool upper_case =
false)
98 static constexpr short char0_9_to_number = 48;
99 static constexpr short charA_F_to_number = 55;
100 static constexpr short chara_f_to_number = 87;
102 size_t in_size = std::distance(begin, end);
103 size_t out_size = in_size << 1;
106 out->resize(out_size);
109 for (CharIterator it = begin; it != end; ++it)
111 short msn = (*it >> 4) & 0x0f;
112 short lsn = *it & 0x0f;
114 if (msn >= 0 && msn <= 9)
115 (*out)[2 * i] = msn + char0_9_to_number;
116 else if (msn >= 10 && msn <= 15)
117 (*out)[2 * i] = msn + (upper_case ? charA_F_to_number : chara_f_to_number);
119 if (lsn >= 0 && lsn <= 9)
120 (*out)[2 * i + 1] = lsn + char0_9_to_number;
121 else if (lsn >= 10 && lsn <= 15)
122 (*out)[2 * i + 1] = lsn + (upper_case ? charA_F_to_number : chara_f_to_number);
128template <
typename CharIterator>
inline std::string hex_encode(CharIterator begin, CharIterator end)
131 hex_encode(begin, end, &out);
140inline void hex_encode(
const std::string& in, std::string* out,
bool upper_case =
false)
142 hex_encode(in.begin(), in.end(), out, upper_case);
145inline std::string hex_encode(
const std::string& in)
148 hex_encode(in, &out);
152inline std::string b64_encode(
const std::string& in)
154 return base64::to_base64(in);
157inline std::string b64_decode(
const std::string& in)
159 return base64::from_base64(in);
166 unsigned r = ((v & (v - 1)) == 0) ? 0 : 1;
182inline double log2(
double d) {
return std::log2(d); }
Dynamic Compact Control Language namespace.
google::protobuf::uint64 uint64
an unsigned 64 bit integer
void hex_encode(CharIterator begin, CharIterator end, std::string *out, bool upper_case=false)
Encodes a (little-endian) hexadecimal string from a byte string. Index 0 of begin is written to index...
void hex_decode(const std::string &in, std::string *out)
Decodes a (little-endian) hexadecimal string to a byte string. Index 0 and 1 (first byte) of in are w...
unsigned ceil_log2(dccl::uint64 v)