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
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);
%ld did it! thank-you ! I am storing the number as a long so that i can save space, 4bytes as apposed to 9bytes. I also dont store the leading 0, but thank-you for preventing me to pull further hair out my head!
So, how would you store a number that starts with 00?'
In what part of the world will you be able to call any other part of the world with just 9 digits, and without any leading zero?
I recommend that you switch to BCD and store two digits/byte.
Since its for a gsm application i strip the leading 0's and add a + to the number, good idea with the BCD, will see how much i need to change :) thanx again
"I recommend that you switch to BCD and store two digits/byte."
I think the term to which you refer is packed BCD ??
Sorry, but I am acting as a pedant due to a result of a typographical error that was detected in one of my earlier posts.
So you add a '+' for international.
The largest possible number for an unsigned long is 4294967295.
How do you then call American Samoa? Country code 1-684 and 7 digits for residential part.
16849999999 is larger than a 32-bit unsigned integer.
GSM itself uses what it calls "semi-octets" - which is effectively just two 4-bit BCD digits packed into each byte (or "octet").
There is also a "type-of-number" field to identify whether it's national, international, etc - without the need to store the '+' or whatever...
If a user is storing a phone number locally, he/she must either store a full international phone number, of make sure to have room to mirror your type-of-number information.