I ran into an issue when I was trying create a code that would convert a decimal to a hexadecimal string. This issue that I'm having is the "string subscript out of range" error. I can't seem to find out where the string conflicts with the range, therefore, I have come here to ask for your help to find out what is causing the error!
This is the code
int i;
int temp;
string hex;
long totalDecimal;
cin >> totalDecimal;
for (i = 0; totalDecimal != 0; i++)
{
temp = totalDecimal % 16;
if (temp < 10)
{
hex[i] = temp + 48;
}
else
{
hex[i] = temp + 55;
}
totalDecimal = totalDecimal / 16;
}
cout << hex;
return 0;
How to fix it?