This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

sprtinff(buff,"%04lx",(unsigned short)number);

Other Parts Discussed in Thread: MSP430G2553

Hello,

I am trying to convert unsigned short to a hex ascii string. It seems that sprintf() fails to do it.
fo example:

static void decimal_to_hex_string(USHORT hex_num, byte * pdecimal_number_str)
{    
    sprintf((char *)pdecimal_number_str, "%04lx", (USHORT)hex_num);
}

int main(void)
{    
    char pdecimal_number_str[5];    
    decimal_to_hex_string(0xBFCA, pdecimal_number_str);
    // ------> After this line pdecimal_number_str is {0x62, 0xff,0xff, 0x45} while it should be {0x62, 0x66,0x63, 0x61}
}

I increased "Level of printf support" to full, and increased "Heap Size for C/C++" and "Set C system stack" to 0x1000 but still doesn't
work. I am usng CCS version 5.1.1.00031 and compiler version 4.0.0.

Is there any limitation for sprintf? Please note that this code works fine in visual studio.
thank you for your help.

  • Hi, 

    I am sufffering at the moment with the same problem. I was trying to convert a double into a string using sprintf and then display it on LCD. It doesnt work though. I am using Code Composer Studio V4. 

    Wish to see more replies in this regard. 

    Best, 

    Vidya.

  • Hello,

    Instead of "%04lx", try "%04ux".  When you use "l", sprintf() assumes you put a 4-byte longword on the stack.  When you use "u", sprintf() assumes you put a 2-byte word on the stack.  Your parameter is a USHORT, which I'm assuming is actually a 2-byte word.

    It works in visual studio because modern PC compilers use 32 bits to pass anything 32 bits or smaller.  Not so for the MSP430 compiler.

    The IAR compiler actually scans the format string and the types of the arguments and gives warnings for mismatches.  Not sure if CCS can do that.

    Jeff

    [Edit: "u" actually means unsigned, so in reality it's the absence of the "l" that tells sprintf() that you put only a 2-byte word on the stack.  So "ul" would be an unsigned 4-byte longword.  And with "x" or "X", the signed/unsigned characteristic doesn't really matter.  Hence "%04x" would work for you too.]

  • Hi ngsoft

    Your code after some changes works for me.

    #include "msp430g2553.h"
    #include "stdio.h"

    static void decimal_to_hex_string( int hex_num,  char * pdecimal_number_str)
    {
        sprintf(( char *)pdecimal_number_str, "%x", ( int)hex_num);
    }

    int main(void)
    {
        char pdecimal_number_str[5];
        decimal_to_hex_string(0xBFCA, pdecimal_number_str);
        // ------> After this line pdecimal_number_str is {0x62, 0xff,0xff, 0x45} while it should be {0x62, 0x66,0x63, 0x61}
      while(1);
    }

    Best Regards,

    AES

  • Hi Vidya,

    Please start a new thread and post your code so forum members can try to help.

    Jeff

  • Hello AES,

    you code works in my enviroment also. THe difference between mine and yours, is that mine adds '0' and your don't.
    for example: dec 500 - mine will show 01f4 while yours will do 1f4.

  • Hello Jeff,

    Thank you also for your reply. In your solution the buffer gets "4ux" instead of getting the real value.
    I read in some places that there was a bug related for sprintf for compiler version 4.0.0 which i am using and which
    it was fixed in later version.

  • Hi ngsoft,

    Wow sorry for the bad information.  Let me try again.

    "%04ux" isn't valid.  My mistake.

    "%04x" is valid.

    The "u" and the "x" are mutually exclusive.  They are both "specifiers", so you must pick just one.

    Jeff

  • hi jeff, 

    it doesnt work with "%04x" too. it displays 4x all the time. 

    i have just seen that, you wanted to post  a new thread, sorry i havent seen this earlier. i have created one sometime before: http://e2e.ti.com/support/microcontrollers/msp43016-bit_ultra-low_power_mcus/f/166/t/195683.aspx^

    thanks a lot for the support

    vidya.

**Attention** This is a public forum