Added Encodings::Hex::decode

This commit is contained in:
Thulinma 2016-09-02 14:31:20 +02:00
parent 2c44be003c
commit 1aeecc53bd
2 changed files with 13 additions and 0 deletions

View file

@ -102,6 +102,16 @@ namespace Encodings {
return r; return r;
} }
/// Decodes a hex-encoded std::string to a raw binary std::string.
std::string Hex::decode(const std::string & in){
std::string ret(in.size()/2, '\000');
for (size_t i = 0; i < in.size(); ++i){
char c = in[i];
ret[i>>1] |= ((c&15) + (((c&64)>>6) | ((c&64)>>3))) << ((~i&1) << 2);
}
return ret;
}
/// urlencodes std::string data, leaving only the characters A-Za-z0-9~!&()' alone. /// urlencodes std::string data, leaving only the characters A-Za-z0-9~!&()' alone.
std::string URL::encode(const std::string & c){ std::string URL::encode(const std::string & c){
std::string escaped = ""; std::string escaped = "";

View file

@ -33,6 +33,9 @@ namespace Encodings {
} }
/// Encodes a single character as two hex digits in string form. /// Encodes a single character as two hex digits in string form.
static std::string chr(char dec); static std::string chr(char dec);
/// Decodes a hex-encoded std::string to a raw binary std::string.
static std::string decode(const std::string & in);
}; };
} }