AntoineLev AntoineLev - 8 days ago 5
C++ Question

casting int to char causes '\x' character

char hexHash[9] = "00000000";
for (int i = 0; i < 8; i++)
{
hexHash[i] = (char)(integerlist[i]);
}
int intHash = stoi(hexHash);


the char conversion causes a \x character so hexHash[0] is '\x2'
this causes my int casting afterward to crash. How do I avoid this?

Answer

If your goal is to place the character representation of a digit in the first element of hexHash, then the following should work:

char hexHash[9] = "00000000";
for (int i = 0; i < 8; i++)
{
    hexHash[0] = i + '0';
}

Adding the character '0' converts the numeric value to its corresponding character.

Live Example