• Join
  • Sign In with my.TI Login
Texas Instruments
  • Products
  • Applications
  • Tools & Software
  • Support & Community
  • Sample & Buy
  • About TI
Sample & Purchase Cart Sample & Purchase Cart
  • Search
  • Advanced
TI E2E™ Community
  • Support Forums
  • Blogs
  • Groups
  • Videos
  • 简体中文
  • More ...
TI Home » TI E2E Community » Support Forums » Microcontrollers » MSP430™ Microcontrollers » MSP430 Ultra-Low Power 16-bit Microcontroller Forum » problem in converting character/int value to string in MSP430F5529
Share
MSP430™ Microcontrollers
  • Forum
  • Announcements
  • E2E Wiki
Options
  • Subscribe via RSS
MSP430 Resources
  • MSP430 Product Folder
  • MSP-EXP430G2 - MSP430 LaunchPad Value Line Development kit
  • MSP430 Getting Started Guide
  • MSP430 Microcontroller Projects
  • More Resources >
  • problem in converting character/int value to string in MSP430F5529

    problem in converting character/int value to string in MSP430F5529

    This question is not answered
    utpal kumar
    Posted by utpal kumar
    on May 09 2012 00:31 AM
    Intellectual570 points

    Hi All,

    I am using MCU MSP430F5529 for my current project,I am getting some difficulty in it.Please find below the problems.

     

    In our project we want to convert character/int value to string, for this we are using sprint() inbuilt function

    Provided by compiler. If the character/int value is less than 10 then we want to append ‘0’

    For e.g. if value is 9 we want in string as “09”. To achieve this we are using the following format

     

    Sprint (Buffer, “%02d%02d%04d%02d%02d%02d, Day,Month,Year,Hour,Min,Secs);

     

    But this is not working we are getting the same content in Buffer as mentioned in format specifier.

    Is there any setting in CCS IDE to achieve this.

    Please help me out of this asap.

     

    Report Abuse
    • Reply
    You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    All Replies
    • Jens-Michael Gross
      Posted by Jens-Michael Gross
      on May 09 2012 08:31 AM
      Guru141810 points

      The default for (s)printf is just basic support. That means, the format string is jsut printed. It doesn't make much sens for sprintf, but is meant for simple debug text output throught he debug console without need to write your own outptu funciton or have all the overhead of a full printf in your binary.

      To enable interpretation of the format string, you must change the project stettings. there mey be different levels of priintf support. Keep in mind that the code behind full printf support is huge, as printf is a very complex function.

      _____________________________________
      Before posting bug reports or ask for help, do at least quick scan over this article. It applies to any kind of problem reporting. On any forum. And/or look here.
      If you cannot discuss your problem in the public, feel free to start a private conversation: click on my name and then 'start conversation'. But please do so only if you really cannot do it in a public thread, as I usually read all threads. And I prefer to answer where others can profit from it (or contribute to it) too.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • eltury
      Posted by eltury
      on May 09 2012 09:27 AM
      Expert1795 points

      Sprint (Buffer, “%02d%02d%04d%02d%02d%02d, Day,Month,Year,Hour,Min,Secs); is missing a ".

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Jens-Michael Gross
      Posted by Jens-Michael Gross
      on May 09 2012 11:24 AM
      Guru141810 points

      eltury
      Sprint (Buffer, “%02d%02d%04d%02d%02d%02d, Day,Month,Year,Hour,Min,Secs); is missing a ".

      I guess, the resulting compiler error would have been noticed, so it's just a typo when writing the post.
      As stated, the program is running, so it compiled. It's just not producing the expected result.

      _____________________________________
      Before posting bug reports or ask for help, do at least quick scan over this article. It applies to any kind of problem reporting. On any forum. And/or look here.
      If you cannot discuss your problem in the public, feel free to start a private conversation: click on my name and then 'start conversation'. But please do so only if you really cannot do it in a public thread, as I usually read all threads. And I prefer to answer where others can profit from it (or contribute to it) too.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • old_cow_yellow
      Posted by old_cow_yellow
      on May 09 2012 11:28 AM
      Guru26615 points

      utpal kumar
      ... I am using MCU MSP430F5529 for my current project,I am getting some difficulty in it... Is there any setting in CCS IDE to achieve this...

      Your problem is not related to the MSP430F5529. It is caused by CCS and the way you use it (or, abuse it).

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • utpal kumar
      Posted by utpal kumar
      on May 09 2012 23:01 PM
      Intellectual570 points

      hi jens,

      thanks for reply.

      i will try it ,and come back with feedback soon.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • utpal kumar
      Posted by utpal kumar
      on May 09 2012 23:33 PM
      Intellectual570 points

      Hi jens,

       

      Missing " in the sprintf function was my typing mistake but in the code it was there. You have mentioned as sprint instead of

      sprintf, this also we tried but giving error as TI doesn’t have sprint library function.  

       

      We have used syntax as follows in code:

       

      sprintf (Buffer, “%02d%02d%04d%02d%02d%02d”, Day,Month,Year,Hour,Min,Secs);       

       

      So please give the solution.

       

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • utpal kumar
      Posted by utpal kumar
      on May 09 2012 23:35 PM
      Intellectual570 points

      hi ocy,

       

      Missing " in the sprintf function was my typing mistake but in the code it was there. You have mentioned as sprint instead of

      sprintf, this also we tried but giving error as TI doesn’t have sprint library function.  

       

      We have used syntax as follows in code:

       

      sprintf (Buffer, “%02d%02d%04d%02d%02d%02d”, Day,Month,Year,Hour,Min,Secs);       

       

      So please give the solution.

       

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Jens-Michael Gross
      Posted by Jens-Michael Gross
      on May 10 2012 09:58 AM
      Guru141810 points

      utpal kumar
      sprintf (Buffer, “%02d%02d%04d%02d%02d%02d”, Day,Month,Year,Hour,Min,Secs);       

      This should work.

      However, you'll have to change the default project settings (not your code) so the linker will include the printf/sprintf code with full funcitonality. The default will just discard all parameters and only print the format string as-is.

      The reason is that the full functionality code is some kb size and won't even fit into the smaller MSPs. It also requires quite some stack space, also not available on some of the smaller MSPs.

      utpal kumar
      You have mentioned as sprint instead of sprintf

      Did I? Not that I know of. I re-read my post in case of a typo, but didn't see one (tho only occurrences of Sprint were in your own post, which I also assumed a typo like the missing "). sprintf() is correct.

      _____________________________________
      Before posting bug reports or ask for help, do at least quick scan over this article. It applies to any kind of problem reporting. On any forum. And/or look here.
      If you cannot discuss your problem in the public, feel free to start a private conversation: click on my name and then 'start conversation'. But please do so only if you really cannot do it in a public thread, as I usually read all threads. And I prefer to answer where others can profit from it (or contribute to it) too.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • TonyKao
      Posted by TonyKao
      on May 10 2012 14:24 PM
      Genius3770 points

      Jens-Michael Gross
      The reason is that the full functionality code is some kb size and won't even fit into the smaller MSPs. It also requires quite some stack space, also not available on some of the smaller MSPs.

      Not sure how sprintf is implemented on CCS but some implementations actually use the Heap, so you'll have to deal with dynamic allocation also...

      The best way IMHO is to use something like itoa() which unfortunately is not available on CCS or IAR, but it's much more compact than sprintf.

      Tony

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Chester Gillon
      Posted by Chester Gillon
      on May 10 2012 16:58 PM
      Mastermind7335 points

      utpal kumar
      So please give the solution.

      Which version of the MSP430 Code Generation Tools are you using?

      Version 4.0.0 has the following bug:

      ------------------------------------------------------------------------------
      FIXED SDSCM00042077
      ------------------------------------------------------------------------------

      Summary : Printf only prints first character of argument correctly

      Fixed in : 4.0.1
      Severity : S2 - Major
      Affected Component : Runtime Support Libraries (RTS)

      Description:
      Printf only prints first character of argument correctly.
      For example, printf("%d\n", 123 ) is printed as 1cÿ

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • old_cow_yellow
      Posted by old_cow_yellow
      on May 10 2012 18:40 PM
      Guru26615 points

      If you only want to convert a small integer (0 to 99) to two ASCII characters, you can simply do:

      first_char = small_int / 10 +'0';

      second_char = small_int % 10 + '0';

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • TonyKao
      Posted by TonyKao
      on May 11 2012 03:53 AM
      Genius3770 points

      old_cow_yellow

      If you only want to convert a small integer (0 to 99) to two ASCII characters, you can simply do:

      first_char = small_int / 10 +'0';

      second_char = small_int % 10 + '0';

      The library fixed point division and modulo add up to almost 100 bytes; in the same space you can implement a more general solution for converting integers to ASCII:

      #include <stdint.h>
      
      // div & mod through series decomposition
      class Modulo
      {
      public:
        uint16_t quotient;
        uint16_t remainder;
        //num = number to be divided, d = divisor
        Modulo(uint16_t num, uint16_t d)
        {
          quotient = 0;
          remainder = num;
          do {
            num -= d;
            if(num > remainder)
              break;
            remainder = num;
            quotient ++;
          } while(num);
        }
      private:
        Modulo();
      };
      
      char* utoa(uint16_t num, char* s)
      {
        int count = 0;
        char buffer[8];
        do
        {
          Modulo result = Modulo(num, 10);
          buffer[count] = '0' + result.remainder;
          count++;
          num = result.quotient;
        } while(num);
        
        int i;
        for(count--,i=0; i < count; i++)
        {
          s[i] = buffer[count - i];
        }
        // null terminate string
        s[i] = '\0';
      
        return s;
      }
      
      int main(void)
      {
        char temp[8];
        utoa(16384, temp);
        // temp is {'1','6','3','8','4','\0'}
        return 0;
      }
      

      The whole thing including main() compiles to about 100 bytes (on IAR). I'm sure it can be further optimized since I didn't spend much time tweaking.

      For an even more general solution, you can "templatize" the factory class, then use specialization in each case of signed/unsigned/int/long/etc. You'd have to specifically check for signs in signed specializations but should be fairly trivial.

      Tony

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Jens-Michael Gross
      Posted by Jens-Michael Gross
      on May 11 2012 08:25 AM
      Guru141810 points

      TonyKao
      The library fixed point division and modulo add up to almost 100 bytes;

      Not to mention the execution time. :)

      Your implementation is nice and is more or less a class-bound, recursive implementazion of my proposals/sample code made earlier in this board.

      _____________________________________
      Before posting bug reports or ask for help, do at least quick scan over this article. It applies to any kind of problem reporting. On any forum. And/or look here.
      If you cannot discuss your problem in the public, feel free to start a private conversation: click on my name and then 'start conversation'. But please do so only if you really cannot do it in a public thread, as I usually read all threads. And I prefer to answer where others can profit from it (or contribute to it) too.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • TonyKao
      Posted by TonyKao
      on May 11 2012 11:55 AM
      Genius3770 points

      Jens-Michael Gross

      Your implementation is nice and is more or less a class-bound, recursive implementazion of my proposals/sample code made earlier in this board.

      I almost squealed unseemly like a schoolgirl. Thanks Jens-Michael :)

      And the implementation is not quite recursive, since there's only once instance of the factory class at any given time; only the computed results are "recursed". The C/C++ language is not meant to be (shudders) functional after all. Although funnily enough, the C++ template semantics can be metaprogrammed functionally, and is actually Turing-complete...

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Norman Wong
      Posted by Norman Wong
      on May 11 2012 15:00 PM
      Guru15120 points

      The Modulo constructor might result in a very large number of repeated subtractions. Worst case for a number of 65535, there would be 6553+655+65+6 = 7279. Here's my implementation of an algorithm purposed by Jens-Michael Gross. It uses repeated subraction of 10^n bases. It also creates the digits in order. Plain C. Not as visually clean are your C++ code.


      /*-----------------------------------------------------------------------------
      Lookup table for units for each position. Order is from largest to smallest.
      Example below assumes "int" is 16 bits. Last value must always be one.
      -----------------------------------------------------------------------------*/
      static const unsigned int g_units[]=
      {
            100000, /* 10^5 */
             10000, /* 10^4 */
              1000, /* 10^3 */
               100, /* 10^2 */
                10, /* 10^1 */
                 1  /* 10^0 */
      };

      /*-----------------------------------------------------------------------------
      Converts signed integer to base 10 string. Caller must allocate enough
      space for result. Returns pointer to beginning of string. Same as passed in.
      Assuming 16 bit signed integers and value is 29999, there will be 38
      subractions.
      -----------------------------------------------------------------------------*/
      char *itod(int v, char *s)
      {
        register char        *p = s;
        register unsigned int vabs;
        register unsigned int unit;
        register int          n;
        register int          i;

        /* Handle 0 as special case. Code below assumes a non-zero value. */
        if(v == 0)
        {
          *p++ = '0';
          *p++ = '\0';
           return(s);
        }

        /* Make the value absolute and emit a '-' is required. */
        if(v < 0)
        {
         *p++  = '-';
          vabs = -v;
        }
        else
          vabs = v;

        /* Find first unit */
        i = 0;
        for(;;)
        {
          unit = g_units[i];
          if(vabs > unit) break;
          i++;
        };

        /* Loop through each unit. */
        for(;;)
        {
          unit = g_units[i];
          if(unit==1) break;

          /* Subtract units until no more of this unit. */
          n = 0;
          while(vabs > unit)
          {
            vabs -= unit;
            n++;
          }

         *p++ = (char)('0' + n); /* Emit character. */
          i++;                   /* Advance index to next unit. */
        }

        /* Last unit is always one. Special case. Finish up. */
       *p++ = (char)('0' + vabs); /* Emit last character. */
       *p++ = '\0';               /* Null terminate.*/

        return(s);
      }

      I don't have a MSP430 platform. Can't say how much code space would be used.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    12
    TI E2E™ Community
    • Support Forums
    • Blogs
    • Videos
    • Groups
    • Site Support & Feedback
    • Settings
    TI E2E™ Community Groups
    • TI University Program
    • Make the Switch
    • Microcontroller Projects
    • Motor Drive & Control
    Other Communities
    • Deyisupport
    • Designsomething.org
    • beagleboard.org
    • TI on Element 14
    • TI on TechXchangeSM
    Other Technical & Support Resources
    • WEBENCH® Design Center
    • Product Information Centers
    • Technical Documents
    • TI Design Network
    • TI Technical Articles
    • TI Training

    All content and materials on this site are provided "as is". TI and its respective suppliers and providers of content make no representations about the suitability of these materials for any purpose and disclaim all warranties and conditions with regard to these materials, including but not limited to all implied warranties and conditions of merchantability, fitness for a particular purpose, title and non-infringement of any third party intellectual property right. TI and its respective suppliers and providers of content make no representations about the suitability of these materials for any purpose and disclaim all warranties and conditions with respect to these materials. No license, either express or implied, by estoppel or otherwise, is granted by TI. Use of the information on this site may require a license from a third party, or a license from TI.

    Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms of Use of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms of Use of this site. TI, its suppliers and providers of content reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

    Follow Us Texas Instruments on Facebook Texas Instruments on Twitter Texas Instruments on LinkedIn Texas Instruments on Google+
    TI Worldwide | Contact Us | my.TI Login | Site Map | Corporate Citizenship | mobile m.ti.com (Mobile Version)

    TI is a global semiconductor design and manufacturing company. Innovate with 100,000+ analog ICs and
    embedded processors, along with software, tools and the industry’s largest sales/support staff.

    © Copyright 1995-2013 Texas Instruments Incorporated. All rights reserved.
    Trademarks | Privacy Policy | Terms of Use