Advertisement
edo_py

hex2txt

Dec 21st, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5. // usage: hex2txt [hex_code]
  6. string func, args;     // global strings
  7.  
  8. string hex2txt(string hex){
  9.    
  10.     hex.erase(0, 8);
  11.    
  12.     if (hex.length() != 0){
  13.        
  14.         unsigned int evenOrOdd = hex.size() % 2;
  15.    
  16.         if (evenOrOdd == 0){
  17.             string text;
  18.    
  19.             for (int i = 0; i < hex.size(); i++){    // erase all spaces from input
  20.                 if (hex[i] == ' ')
  21.                     hex.erase(i, 1);
  22.             }
  23.    
  24.             string hex_part;
  25.    
  26.             char* hexTable [256] =
  27.             {
  28.                 "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "2a", "2b", "2c", "2d", "2e", "2f", "30", "31",
  29.                 "32", "33", "34", "35", "36", "37", "38", "39", "3a", "3b", "3c", "3d", "3e", "3f", "40", "41", "42", "43",
  30.                 "44", "45", "46", "47", "48", "49", "4a", "4b", "4c", "4d", "4e", "4f", "50", "51", "52", "53", "54", "55",
  31.                 "56", "57", "58", "59", "5a", "5b", "5c", "5d", "5e", "5f", "60", "61", "62", "63", "64", "65", "66", "67",
  32.                 "68", "69", "6a", "6b", "6c", "6d", "6e", "6f", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79",
  33.                 "7a", "7b", "7c", "7d", "7e", "7f"
  34.             };
  35.    
  36.             char* alphanumTable [256] =
  37.             {
  38.                 " ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5",
  39.                 "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
  40.                 "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a",
  41.                 "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
  42.                 "x", "y", "z", "{", "|", "}", "~"
  43.             };
  44.    
  45.             for (int i = 0; i < hex.size(); i+=2){    // translate hex to text
  46.        
  47.                 hex_part.push_back(hex[i]);
  48.                 hex_part.push_back(hex[i+1]);
  49.        
  50.                 for (int e = 0; e < hex.size(); e++){
  51.                     if (hex_part == hexTable[e])
  52.                         strcpy(hexTable[e], text.c_str());
  53.                     else
  54.                         continue;
  55.                 }
  56.        
  57.             hex_part.erase(0, 2);
  58.             return text;
  59.    
  60.             }
  61.         }
  62.         else
  63.             return "ERROR: Invalid HEX code.";
  64.     }
  65.     else
  66.         return "ERROR: Missing HEX code.";
  67. }
  68.  
  69. int main(){
  70. //  if (func == "hex2txt"){
  71.     cout << hex2txt("hex2txt 2b3c4f") << endl;   // example
  72. //  }
  73.     system("PAUSE");
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement