如何修复隐式有符号转无符号转换(Warning 373)问题
解决XC8编译器警告373的问题
我知道这个问题已被问过几次,但之前的答案不够清晰。该警告似乎与编译器将char类型提升为unsigned char有关……我在下方函数的两处str[numbytes-i] = r + 0x30;位置收到该警告,正确的解决方法是什么?
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"
更新
感谢大家的建议和指导。@chux的替代代码、@Persixty的简洁方案都很实用。按建议进行类型转换后问题已解决。
补充代码背景:我在硬件重设计项目中接手此代码,客户称其为良好起点,实际却一团糟。代码用MPLAB、XC8编译器编写,存在超500条警告、注释问题、编码规范差等问题,经了解是多人修改的遗留产物,大写类型名是原作者自定义的。
内容的提问来源于stack exchange,提问作者Gerald Eberhardt
相关产品推荐
相关产品推荐

