<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>CodeWarrior for MCUのトピックRe: Number to String Conversion</title>
    <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Number-to-String-Conversion/m-p/131989#M1226</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;P&gt;You can use sprintf function that is part of the std C lib.&lt;/P&gt;&lt;P&gt;Or I made the following code to build a String with a converted number added to it using the following code. The first function combines some of the standard C string functions along with the Integer to Ascii conversion. I use integers in C with assumed decimal points. This is limited as to the number of digits. The code can be modified to use long intergers if needed.&lt;/P&gt;&lt;P&gt;Example - I have a timer value that as an integer it is 9999. But as the user sees it after conversion it is 9.999&lt;/P&gt;&lt;P&gt;I define the Digits and Decimal positions&lt;/P&gt;&lt;P&gt;#define DELAY_TIMER_DIGITS 5&lt;/P&gt;&lt;P&gt;#define DELAY_TIMER_DECIMAL 1&lt;/P&gt;&lt;P&gt;int TimerValue = 9999;&lt;/P&gt;&lt;P&gt;char MyString[30];&lt;/P&gt;&lt;P&gt;To build a string to display to user with format of x.xxx for TimerValue&lt;/P&gt;&lt;P&gt;ItoaMsg(&amp;amp;MyString[0], "Delay Timer = ", TimerValue, DELAY_TIMER_DIGITS, DELAY_TIMER_DECIMAL);&lt;/P&gt;&lt;P&gt;I then print MyString on the display which would be "Delay Timer = 9.999"&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;void ItoaMsg(char *Buffer, char *Msg, INT16 Number, char Digits, char Decimal) {&lt;BR /&gt;&amp;nbsp;/* Local variables */&lt;BR /&gt;&amp;nbsp;char Array[11];&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Convert Number to string */&lt;BR /&gt;&amp;nbsp;Itoa(Number, Digits, Decimal, &amp;amp;Array[0]);&lt;BR /&gt;&amp;nbsp;/* Copy Msg to Buffer */&lt;BR /&gt;&amp;nbsp;(void) strcpy(Buffer, Msg);&lt;BR /&gt;&amp;nbsp;/* Add Number string to Buffer */&lt;BR /&gt;&amp;nbsp;(void) strcat(Buffer, &amp;amp;Array[0]);&lt;BR /&gt;}&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;/* Integer to ASCII */&lt;BR /&gt;void Itoa(INT16 Number, char Digits, char Decimal, char *String)&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp;/* Get starting position in string array */&lt;BR /&gt;&amp;nbsp;char *PositionSave, *Position = String + Digits;&lt;BR /&gt;&amp;nbsp;/* Negative flag */&lt;BR /&gt;&amp;nbsp;char NegativeFlag = FALSE;&lt;BR /&gt;&amp;nbsp;/* Charactor buffer */&lt;BR /&gt;&amp;nbsp;char Chr;&lt;BR /&gt;&amp;nbsp;/* Working UINT16 */&lt;BR /&gt;&amp;nbsp;UINT16 uInteger;&lt;BR /&gt;&amp;nbsp;/* Loop index */&lt;BR /&gt;&amp;nbsp;INT16 Index;&lt;BR /&gt;&amp;nbsp;/* Number of digits converted */&lt;BR /&gt;&amp;nbsp;INT16 DigitCount = 0;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;/* Make sure we are converting more than one digit */&lt;BR /&gt;&amp;nbsp;if(!Digits)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;return;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;/* Are we working with a negative number? */&lt;BR /&gt;&amp;nbsp;if (Number &amp;lt; 0) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* It is a negative number */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;NegativeFlag = TRUE;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Remove the sign bit from the integer */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;uInteger = ((UINT16)(-(1 + Number))) + 1;&lt;BR /&gt;&amp;nbsp;} else {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Not a negative number */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;uInteger = Number;&lt;BR /&gt;&amp;nbsp;}&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;/* NULL terminate the string */&lt;BR /&gt;&amp;nbsp;*Position = 0;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;/* Do the conversion */&lt;BR /&gt;&amp;nbsp;do {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Decrement the digits counter */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;Digits--;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Get the ASCII charactor */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;*--Position = '0' + (uInteger % 10);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Divide by 10 */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;uInteger /= 10;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Have we reached the end of the number? */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;if(!uInteger)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;break;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Increment the digit counter */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;DigitCount++;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;} while (Digits);&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;/* Do we need to add a decimal point? */&lt;BR /&gt;&amp;nbsp;if(Decimal &amp;amp;&amp;amp; Digits) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Remove another digit position */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;Digits--;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Do we have enough space for a leading 0? */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;if(Digits &amp;amp;&amp;amp; (DigitCount &amp;lt; Decimal)){&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;for(Index = Decimal - DigitCount; Index &amp;gt; 0; Index--) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*--Position = '0';&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;DigitCount++;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Digits--;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(!Digits)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;break;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR /&gt;&amp;nbsp;&amp;nbsp;}&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Decrement and save the pointer */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;Position--;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;PositionSave = Position;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Move the charactors */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;for(Index = DigitCount - Decimal; Index &amp;gt;= 0; Index--) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Chr = Position[1];&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Position[0] = Chr;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Position++;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;}&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Insert the decimal point */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;*Position = '.';&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Restore the pointer */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;Position = PositionSave;&lt;BR /&gt;&amp;nbsp;}&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;/* Do we need to add the negative sign? */&lt;BR /&gt;&amp;nbsp;if (NegativeFlag &amp;amp;&amp;amp; Digits) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Add the negative charator */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;*--Position = '-';&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Reduce the digit count */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;Digits--;&lt;BR /&gt;&amp;nbsp;}&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;/* Do we need to pad any charactor positions? */&lt;BR /&gt;&amp;nbsp;for(Index = Digits; Index != 0; Index--)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;*--Position = ' ';&lt;BR /&gt;}&lt;BR /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 11 Apr 2006 04:03:55 GMT</pubDate>
    <dc:creator>Technoman64</dc:creator>
    <dc:date>2006-04-11T04:03:55Z</dc:date>
    <item>
      <title>Number to String Conversion</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Number-to-String-Conversion/m-p/131988#M1225</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi all,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am using the 68HC912B32 EVB and CodeWarrior v3.1. I want to convert two numbers in one string. the first one is 1byte(unsigned char) and the other is 4(unsigned long). Is there an implementation in c I could use?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;thanks for the help...&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Apr 2006 01:34:15 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Number-to-String-Conversion/m-p/131988#M1225</guid>
      <dc:creator>Anastasios</dc:creator>
      <dc:date>2006-04-11T01:34:15Z</dc:date>
    </item>
    <item>
      <title>Re: Number to String Conversion</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Number-to-String-Conversion/m-p/131989#M1226</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;P&gt;You can use sprintf function that is part of the std C lib.&lt;/P&gt;&lt;P&gt;Or I made the following code to build a String with a converted number added to it using the following code. The first function combines some of the standard C string functions along with the Integer to Ascii conversion. I use integers in C with assumed decimal points. This is limited as to the number of digits. The code can be modified to use long intergers if needed.&lt;/P&gt;&lt;P&gt;Example - I have a timer value that as an integer it is 9999. But as the user sees it after conversion it is 9.999&lt;/P&gt;&lt;P&gt;I define the Digits and Decimal positions&lt;/P&gt;&lt;P&gt;#define DELAY_TIMER_DIGITS 5&lt;/P&gt;&lt;P&gt;#define DELAY_TIMER_DECIMAL 1&lt;/P&gt;&lt;P&gt;int TimerValue = 9999;&lt;/P&gt;&lt;P&gt;char MyString[30];&lt;/P&gt;&lt;P&gt;To build a string to display to user with format of x.xxx for TimerValue&lt;/P&gt;&lt;P&gt;ItoaMsg(&amp;amp;MyString[0], "Delay Timer = ", TimerValue, DELAY_TIMER_DIGITS, DELAY_TIMER_DECIMAL);&lt;/P&gt;&lt;P&gt;I then print MyString on the display which would be "Delay Timer = 9.999"&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;void ItoaMsg(char *Buffer, char *Msg, INT16 Number, char Digits, char Decimal) {&lt;BR /&gt;&amp;nbsp;/* Local variables */&lt;BR /&gt;&amp;nbsp;char Array[11];&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Convert Number to string */&lt;BR /&gt;&amp;nbsp;Itoa(Number, Digits, Decimal, &amp;amp;Array[0]);&lt;BR /&gt;&amp;nbsp;/* Copy Msg to Buffer */&lt;BR /&gt;&amp;nbsp;(void) strcpy(Buffer, Msg);&lt;BR /&gt;&amp;nbsp;/* Add Number string to Buffer */&lt;BR /&gt;&amp;nbsp;(void) strcat(Buffer, &amp;amp;Array[0]);&lt;BR /&gt;}&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;/* Integer to ASCII */&lt;BR /&gt;void Itoa(INT16 Number, char Digits, char Decimal, char *String)&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp;/* Get starting position in string array */&lt;BR /&gt;&amp;nbsp;char *PositionSave, *Position = String + Digits;&lt;BR /&gt;&amp;nbsp;/* Negative flag */&lt;BR /&gt;&amp;nbsp;char NegativeFlag = FALSE;&lt;BR /&gt;&amp;nbsp;/* Charactor buffer */&lt;BR /&gt;&amp;nbsp;char Chr;&lt;BR /&gt;&amp;nbsp;/* Working UINT16 */&lt;BR /&gt;&amp;nbsp;UINT16 uInteger;&lt;BR /&gt;&amp;nbsp;/* Loop index */&lt;BR /&gt;&amp;nbsp;INT16 Index;&lt;BR /&gt;&amp;nbsp;/* Number of digits converted */&lt;BR /&gt;&amp;nbsp;INT16 DigitCount = 0;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;/* Make sure we are converting more than one digit */&lt;BR /&gt;&amp;nbsp;if(!Digits)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;return;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;/* Are we working with a negative number? */&lt;BR /&gt;&amp;nbsp;if (Number &amp;lt; 0) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* It is a negative number */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;NegativeFlag = TRUE;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Remove the sign bit from the integer */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;uInteger = ((UINT16)(-(1 + Number))) + 1;&lt;BR /&gt;&amp;nbsp;} else {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Not a negative number */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;uInteger = Number;&lt;BR /&gt;&amp;nbsp;}&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;/* NULL terminate the string */&lt;BR /&gt;&amp;nbsp;*Position = 0;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;/* Do the conversion */&lt;BR /&gt;&amp;nbsp;do {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Decrement the digits counter */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;Digits--;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Get the ASCII charactor */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;*--Position = '0' + (uInteger % 10);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Divide by 10 */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;uInteger /= 10;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Have we reached the end of the number? */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;if(!uInteger)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;break;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Increment the digit counter */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;DigitCount++;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;} while (Digits);&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;/* Do we need to add a decimal point? */&lt;BR /&gt;&amp;nbsp;if(Decimal &amp;amp;&amp;amp; Digits) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Remove another digit position */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;Digits--;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Do we have enough space for a leading 0? */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;if(Digits &amp;amp;&amp;amp; (DigitCount &amp;lt; Decimal)){&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;for(Index = Decimal - DigitCount; Index &amp;gt; 0; Index--) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*--Position = '0';&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;DigitCount++;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Digits--;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(!Digits)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;break;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR /&gt;&amp;nbsp;&amp;nbsp;}&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Decrement and save the pointer */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;Position--;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;PositionSave = Position;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Move the charactors */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;for(Index = DigitCount - Decimal; Index &amp;gt;= 0; Index--) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Chr = Position[1];&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Position[0] = Chr;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Position++;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;}&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Insert the decimal point */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;*Position = '.';&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Restore the pointer */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;Position = PositionSave;&lt;BR /&gt;&amp;nbsp;}&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;/* Do we need to add the negative sign? */&lt;BR /&gt;&amp;nbsp;if (NegativeFlag &amp;amp;&amp;amp; Digits) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Add the negative charator */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;*--Position = '-';&lt;BR /&gt;&amp;nbsp;&amp;nbsp;/* Reduce the digit count */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;Digits--;&lt;BR /&gt;&amp;nbsp;}&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;/* Do we need to pad any charactor positions? */&lt;BR /&gt;&amp;nbsp;for(Index = Digits; Index != 0; Index--)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;*--Position = ' ';&lt;BR /&gt;}&lt;BR /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Apr 2006 04:03:55 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Number-to-String-Conversion/m-p/131989#M1226</guid>
      <dc:creator>Technoman64</dc:creator>
      <dc:date>2006-04-11T04:03:55Z</dc:date>
    </item>
  </channel>
</rss>

