title: How to fix implicit signed to unsigned conversion, warning 373
body: <p>I know this has been asked a couple times, but the answers were not clear to me.</p>
  <p>This warning seems to be relate to the type char being promoted by the compiler to unsigned char.....</p>
  <p>I get the warning in two places in the function below at <code>str[numbytes-i] = r + 0x30;</code>.</p>
  <p>What is the proper way to address this issue?</p>
  <pre><code>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 &lt;= 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 &lt;= 9) //Values &lt; 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 &gt; 9
          {
              //The MOD of the number represents the Hex value
              //For values 0-9 the ASCII offset is 0x41 to get to &quot;A&quot; 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 &quot;usitoh&quot;
  </code></pre>
  <p>UPDTAE:</p>
  <p>Thanks everyone for the suggestion and advice.</p>
  <p>@chux I really like your “alternate code” and @Persixty the cleaner option.</p>
  <p>Type casing as suggested fixed this issue.</p>
  <p>A little backstory on the code.</p>
  <p>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.</p>
  <p>The code was written in MPLAB, XC8 compiler.</p>
  <p>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.</p>
  <ol>
  <li>Over 500 warnings</li>
  <li>Poor, missing or inaccurate comments</li>
  <li>Poor and inconsistent coding practices.</li>
  </ol>
  <p>We kept running into weird crashes and compiling issues.</p>
  <p>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.</p>
  <p>So, the upper-case types are an artifact of the original author. Who had defined them in a header file of their own.</p>