Advertisement
Guest User

BiteRepresent

a guest
Oct 22nd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. /*
  2. *   Module Name: BiteRepresent
  3. *   Use: Converts a byte vector to a string and reverses.
  4. *   Developer: Donkey Death Jam
  5. */
  6.  
  7. #include "BiteRepresent.h"
  8.  
  9. map<int, string> BiteRepresent::_NUMBERKEYMAP;
  10. map<string, int> BiteRepresent::_STRINGKEYMAP;
  11.  
  12. void BiteRepresent::Initialize()
  13. {
  14.     _NUMBERKEYMAP[0] = "A";
  15.     _NUMBERKEYMAP[1] = "B";
  16.     _NUMBERKEYMAP[2] = "C";
  17.     _NUMBERKEYMAP[3] = "D";
  18.     _NUMBERKEYMAP[4] = "E";
  19.     _NUMBERKEYMAP[5] = "F";
  20.     _NUMBERKEYMAP[6] = "G";
  21.     _NUMBERKEYMAP[7] = "H";
  22.     _NUMBERKEYMAP[8] = "I";
  23.     _NUMBERKEYMAP[9] = "J";
  24.     _STRINGKEYMAP["A"] = 0;
  25.     _STRINGKEYMAP["B"] = 1;
  26.     _STRINGKEYMAP["C"] = 2;
  27.     _STRINGKEYMAP["D"] = 3;
  28.     _STRINGKEYMAP["E"] = 4;
  29.     _STRINGKEYMAP["F"] = 5;
  30.     _STRINGKEYMAP["G"] = 6;
  31.     _STRINGKEYMAP["H"] = 7;
  32.     _STRINGKEYMAP["I"] = 8;
  33.     _STRINGKEYMAP["J"] = 9;
  34. }
  35.  
  36. string BiteRepresent::ByteToString(vector<BYTE> input)
  37. {
  38.     string byteString;
  39.     for (size_t i = 0; i < input.size(); i++)
  40.     {
  41.         string stringByte = to_string((int)input[i]);
  42.         string stringRep = GetStringRepresentation(stringByte);
  43.         byteString.append(stringRep + ":");
  44.     }
  45.     return byteString.substr(0, byteString.length() - 1);
  46. }
  47.  
  48. vector<BYTE> BiteRepresent::StringToByte(string input)
  49. {
  50.     vector<BYTE> bytes;
  51.     vector<string> stringBytes = SplitString(input, ':');
  52.     for (size_t i = 0; i < stringBytes.size(); i++)
  53.         bytes.push_back(GetByteRepresentation(stringBytes[i]));
  54.     return bytes;
  55. }
  56.  
  57. string BiteRepresent::GetStringRepresentation(string input)
  58. {
  59.     string rep;
  60.     for (size_t i = 0; i < input.length(); i++)
  61.     {
  62.         string character(1, input[i]);
  63.         int byteNum = stoi(character);
  64.         if (_NUMBERKEYMAP.count(byteNum) > 0)
  65.             rep.append(_NUMBERKEYMAP[byteNum]);
  66.     }
  67.     return rep;
  68. }
  69.  
  70. BYTE BiteRepresent::GetByteRepresentation(string input)
  71. {
  72.     string rep;
  73.     for (size_t i = 0; i < input.length(); i++)
  74.     {
  75.         string character(1, input[i]);
  76.         if (_STRINGKEYMAP.count(character) > 0)
  77.             rep.append(to_string(_STRINGKEYMAP[character]));
  78.     }
  79.     return (BYTE)stoi(rep);
  80. }
  81.  
  82. vector<string> BiteRepresent::SplitString(string text, char sep) {
  83.     std::vector<std::string> tokens;
  84.     std::size_t start = 0, end = 0;
  85.     while ((end = text.find(sep, start)) != std::string::npos) {
  86.         tokens.push_back(text.substr(start, end - start));
  87.         start = end + 1;
  88.     }
  89.     tokens.push_back(text.substr(start));
  90.     return tokens;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement