Guest User

Untitled

a guest
Mar 20th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. #include <string>
  2. using std::string;
  3.  
  4. inline string to_hex(unsigned char v){
  5. char buf[3]={0,0,0};
  6. sprintf(buf,"%02x",v);
  7. return buf;
  8. }
  9.  
  10. inline char from_hex(char c1,char c2){
  11. char buf[3]={c1,c2,0};
  12. size_t v;
  13. sscanf(buf,"%02x",&v);
  14. return (char&)v;
  15. }
  16.  
  17. inline string str2hex(const string&s)
  18. {
  19. string out;
  20. out.reserve(s.size()*2);
  21. for(size_t i=0;i<s.size();i++)out+=to_hex((unsigned char&)s[i]);
  22. return out;
  23. }
  24.  
  25. inline string hex2str(const string&s){
  26. string out;
  27. out.reserve(s.size()/2);
  28. for(size_t i=0;i<s.size();i+=2){
  29. out.push_back(from_hex(s[i],s[i+1]));
  30. }
  31. return out;
  32. }
Add Comment
Please, Sign In to add comment