Menu

#19 problem with string to longs

closed
nobody
None
5
2012-04-11
2012-02-28
Philip Man
No

I currently have a problem converting strings to longs. I tried atol, strtol, strtoul. Only atoi seems to work.
sprintf longs using %lu does not seem to work, but ltoa does.

To test these functions, I use something like
sprintf(returnBuffer, "12345");
LCD_WriteString(returnBuffer, 5, 0, 1);
userID = atol(returnBuffer);
// userID = (long) 30010; // test if ltoa/sprintf works
ltoa(userID, returnBuffer, 10);
LCD_WriteString(returnBuffer, 6, 0, 1);

The LCD values are not the same. First display is correct, but second only shows "0".

Discussion

  • Peter A. Bigot

    Peter A. Bigot - 2012-02-28

    If you compile with -Wall, do you see:

    test.c:25:5: warning: implicit declaration of function ‘atol’ [-Wimplicit-function-declaration]
    test.c:27:5: warning: implicit declaration of function ‘ltoa’ [-Wimplicit-function-declaration]

    If so, you need to #include <stdlib.h>, otherwise the compiler assumes ltoa returns a (16-bit) int, and you will only see the high 16 bits of the return value, which are zero in this case.

     
  • Peter A. Bigot

    Peter A. Bigot - 2012-02-28
    • status: open --> closed
     
  • Philip Man

    Philip Man - 2012-02-29

    -Wall was really useful. Now project is cleaned up.
    Unsigned Long functions are now working.
    Thanks.

     
  • Philip Man

    Philip Man - 2012-02-29
    • status: closed --> open
     
  • Peter A. Bigot

    Peter A. Bigot - 2012-04-11
    • status: open --> closed
     

Log in to post a comment.