Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- using std::string;
- inline string to_hex(unsigned char v){
- char buf[3]={0,0,0};
- sprintf(buf,"%02x",v);
- return buf;
- }
- inline char from_hex(char c1,char c2){
- char buf[3]={c1,c2,0};
- size_t v;
- sscanf(buf,"%02x",&v);
- return (char&)v;
- }
- inline string str2hex(const string&s)
- {
- string out;
- out.reserve(s.size()*2);
- for(size_t i=0;i<s.size();i++)out+=to_hex((unsigned char&)s[i]);
- return out;
- }
- inline string hex2str(const string&s){
- string out;
- out.reserve(s.size()/2);
- for(size_t i=0;i<s.size();i+=2){
- out.push_back(from_hex(s[i],s[i+1]));
- }
- return out;
- }
Add Comment
Please, Sign In to add comment