2011-08-25 07:49 AM
Hello,
I have a buffer char buf[128]; And when I use ADC it gave me values like 1204 for eg. by ADC1ConvertedValue[1] which is the channel 1 for me. How could I store the 4 characters 1,2,0 and 4 into my buffer? I have tried buf[1]=ADC1ConvertedValue[1]; but this store only the first character which is normal. thanks2011-08-25 08:08 AM
Presuming ADC1Converted is an array of integers, of range 0-9999, a quick binary to decimal-ASCII conversion.
buf[0]='0' + (char)(ADC1ConvertedValue[1] / 1000); buf[1]='0' + (char)((ADC1ConvertedValue[1] / 100) % 10); buf[2]='0' + (char)((ADC1ConvertedValue[1] / 10) % 10); buf[3]='0' + (char)(ADC1ConvertedValue[1] % 10); buf[4] = 0; http://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628 http://www.amazon.com/Primer-Plus-5th-Stephen-Prata/dp/0672326965 http://www.amazon.com/Absolute-Beginners-Guide-C-2nd/dp/06723051002011-08-26 12:40 AM
thanks clive.
I understand that the first four are forthousand,
hundred
,decade,
unit
but for what buf[4] = 0; is used for? I don't understand the last... Thanks2011-08-26 06:07 AM
The 'buf[4] = 0;' is used to create a NUL terminated string, so you can use other standard C string functions to copy, concatenate, etc the 'buf' content.
It would permit things like puts(buf) to work. http://en.wikipedia.org/wiki/C_string http://cplus.about.com/od/learningc/ss/strings.htm