This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

conversion from long to ascii

Hey Guys, i am storing a number as a long in memory and need to convert it to an ascii number ie 123456789 to "123456789" been trying with :

sprintf(telno,"%u",123456789);

but what gets stored in telno is just [49,56,56,51,0,0,0,0,... which is "1883" which seems to be rubbish to me

here the code:

unsigned long xdata Telephone;
unsigned char xdata telno[16];
main () { Telephone = 123456789; sprintf(telno,"%u",Telephone);
}

thanx in advance

Xarion

Parents
  • First off: Do not (!) try to store telephone numbers as integers. What do you think would happen with a leading zero?

    Second: When you do play with long numbers, you have to specify to the compiler that they are long (or unsigned long).

    long 123456789l; // The letter l att the end.
    unsigned long 123456789ul;
    

    When printing a long number using printf(), sprintf(), ..., you have to specify in the formatting string that you are printing a long value.

    unsigned long my_ulong = 1234567ul;
    printf("%ld",1234567l);
    printf("%lu",my_ulong);
    printf("%lx",my_ulong);
    

Reply
  • First off: Do not (!) try to store telephone numbers as integers. What do you think would happen with a leading zero?

    Second: When you do play with long numbers, you have to specify to the compiler that they are long (or unsigned long).

    long 123456789l; // The letter l att the end.
    unsigned long 123456789ul;
    

    When printing a long number using printf(), sprintf(), ..., you have to specify in the formatting string that you are printing a long value.

    unsigned long my_ulong = 1234567ul;
    printf("%ld",1234567l);
    printf("%lu",my_ulong);
    printf("%lx",my_ulong);
    

Children