#include #include #include int isalpha(int value) { if(value == 0x5b) return 1; if(value == '\n') return 1; if(value == '#') return 1; if(value == ' ') return 1; if(value < 'A') return 0; if(value > 'z') return 0; if( (value > 'Z') && (value < 'a') ) return 0; return true; } struct eqint { bool operator()(const char c1, const char c2) { if(c1 == c2) return true; return false; } }; int hextoi(char* input) { // Convert a 2 character hex string to an int. int total = 0; if( ('a' <= input[0]) && (input[0] <= 'f') ) { total += (16 * (input[0] - 'a' + 10)); } else { total += (16 * (input[0] - '0')); cout << dec << total << endl; } if( ('a' <= input[1]) && (input[1] <= 'f') ) { total += (input[1] - 'a' + 10); } else { total += (input[1] - '0'); } cout << input << " is " << total << endl; return total; } int main(int argc, char** argv) { ifstream file(argv[1]); if(!file) { cerr << "can't open " << argv[1] << "." << endl; exit(1); } hash_map, eqint> characters; char buffer[1024]; file.read(buffer, 1024); int size = file.gcount(); cout << "Read " << size << " bytes." << endl; for(int count = 0;count < 532;count++) { char value = buffer[count]; if(characters.end() == characters.find(value)) { characters[value] = value; } } while(1) { hash_map, eqint>::iterator itr = characters.begin(); hash_map, eqint>::iterator stop = characters.end(); for(;itr != stop;itr++) { cout << hex << itr->first << " maps to " << itr->second << "." << endl; } for(int count = 0;count < size;count++) { char value = buffer[count]; char map_value = characters[value]; if(isalpha(characters[value]) || ('\n' == characters[value])) { cout << characters[value]; } else { // cout << (char) characters[value] << " is not printable. " << endl; cout << "*"; } } char inbuf[1024]; cin.getline(inbuf, 1024, '\n'); int from = hextoi(strtok(inbuf, " \t\r\n")); int to = hextoi(strtok(NULL, " \t\r\n")); characters[from] = to; cout << "Setting " << hex << from << " to " << to << dec << "." << endl; } }