I know this has been asked a couple times, but the answers were not clear to me.
This warning seems to be relate to the type char being promoted by the compiler to unsigned char.....
I get the warning in two places in the function below at str[numbytes-i] = r + 0x30;
.
What is the proper way to address this issue?
void usitoh( UINT16 val, UINT8 numbytes, char *str ) //extern
{
UINT8 i = 0;
UINT8 r = 0;
//Convert the Base 10 Number to Base 16
//Starting with LSB (i=1) and work to the MSB
for (i=1; i <= numbytes; i++)
{
r = val % 16; //Get the MOD of the number
//The MOD of the number represents the Hex value
val = val / 16; //Get the Quotient of the number
//Load the value into the string and
//add an offset to generate ASCII values
// '0' = 0x30, 'A' = 0x41
if (r <= 9) //Values < A
{
//For values 0-9 the ASCII offset is 0x30
//This produces and offset to get to chars 0,1,2,3,4,5,6,7,8,9
//Warning 373 issue
str[numbytes-i] = r + 0x30;
}
else //Values > 9
{
//The MOD of the number represents the Hex value
//For values 0-9 the ASCII offset is 0x41 to get to "A" and - 0x0A to account 0-9 numbers
//This produces and offset to get to chars A,B,C,D,E,F
//Warning 373 issue
str[numbytes-i] = r + (0x41 - 0x0A);
}
}//END of FOR Loop
}//END of "usitoh"
UPDTAE:
Thanks everyone for the suggestion and advice.
@chux I really like your “alternate code” and @Persixty the cleaner option.
Type casing as suggested fixed this issue.
A little backstory on the code.
I inherited this code as part of a project to redesign some hardware, and the client lead us to believe the code was good starting point…. Well… we took the bait, hook, line and sinker.
The code was written in MPLAB, XC8 compiler.
The hardware was quick and easy, but once we started into modifying the code we found that it as epic mess! I have never seen C-code that was so spaghetti-ed, it felt like assembly code. As we continued deeper we found several issues.
- Over 500 warnings
- Poor, missing or inaccurate comments
- Poor and inconsistent coding practices.
We kept running into weird crashes and compiling issues.
After some prodding we found out that code was started by an entrepreneur, handed off to a consulting group who had several people hack at it.
So, the upper-case types are an artifact of the original author. Who had defined them in a header file of their own.