I'm fairly new to c/c++ programming and currently I'm working on some basic programms to get in touch with the language. My newest programm is a simple hex_xor function, that follows the instruction of Cryptopals Challenge 2. But I'm already getting errors and I'm assuming I am doing something horribly wrong with the pointers I am using.
Here is a part of my programm:
const char* hex_char_to_bin(char c)
{
switch(toupper(c))
{
case '0': return "0000";
case '1': return "0001";
case '2': return "0010";
case '3': return "0011";
case '4': return "0100";
case '5': return "0101";
case '6': return "0110";
case '7': return "0111";
case '8': return "1000";
case '9': return "1001";
case 'A': return "1010";
case 'B': return "1011";
case 'C': return "1100";
case 'D': return "1101";
case 'E': return "1110";
case 'F': return "1111";
}
}
const char* hex_binary(const char* c){
std::string result = "";
for(int i = 0; i < strlen(c); i++){
result += hex_char_to_bin(c[i]);
}
return result.c_str();
}
int main(){
std::string s1 = "1c0111001f010100061a024b53535009181c";
std::string s2 = "686974207468652062756c6c277320657965";
const char* bin1 = hex_binary(s1.c_str());
const char* bin2 = hex_binary(s2.c_str());
std::cout << bin1 << "\n" << bin2 << std::endl;
return 0;
}
The output is the following:
011010000110100101110100001000000111010001101000011001010010000001100010011101010110110001101100001001110111001100100000011001010111100101100101
011010000110100101110100001000000111010001101000011001010010000001100010011101010110110001101100001001110111001100100000011001010111100101100101
In both variables (bin1/2) is the binary conversion of the second hex-string. My aim is (obviously) to have both binary-strings saved in different variables, so I can proceed with my xor-function. Can someone point out where I am failing to achieve my goal and why? Other hints are welcome aswell!