470,874 Members | 1,634 Online

# Obtaining digits from bit string

Im working on an embedded project and i have to obtain individual
digits from a byte thats the output of a device. Im trying to output
this info to an LCD and need to convert it to ASCII first. I have the
following which only works for single digits but not double digits.
(Data from chip is stored in current[] array and needs to be converted
and stored in array hours[]).

hours[0] = 0x30 | ((current[2] & 0x70) >> 4); //obtain the tens minute
digit
//current[2] stores the byte i want to convert,

hours[1] = 0x30 | (current[2] & 0x0F) //obtain the ones minute digit.
;

This only works if i have single digit output but i a value like 36
minutes was read from the device, the output goes crazy. I appreciate
any suggestions/ideas.

zion.

Apr 15 '06 #1
5 1759
On 14 Apr 2006 19:17:14 -0700, "zion_zii" <ni*******@gmail.com> wrote
in comp.lang.c:
Im working on an embedded project and i have to obtain individual
digits from a byte thats the output of a device. Im trying to output
this info to an LCD and need to convert it to ASCII first. I have the
following which only works for single digits but not double digits.
(Data from chip is stored in current[] array and needs to be converted
and stored in array hours[]).

hours[0] = 0x30 | ((current[2] & 0x70) >> 4); //obtain the tens minute
digit
//current[2] stores the byte i want to convert,

hours[1] = 0x30 | (current[2] & 0x0F) //obtain the ones minute digit.
;

This only works if i have single digit output but i a value like 36
minutes was read from the device, the output goes crazy. I appreciate
any suggestions/ideas.

zion.

What are the types of "current" and "hours"? What are the values in
"current" when you get what you consider correct output, and what is
that correct output? What are the values in "current" when you get
what you consider "crazy" output, and what is that output?

Exactly how is the format of the data in "current" defined?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Apr 15 '06 #2
I apologize for being so vague.

current and hours are both int ( 8 - bit ints) arrays. The correct
output appears when i have single digits. Prior to what is shown above,
the data is gathered from a terminal and fed into a chip (ds1305),
where a few minuites later, it is read from the same chip and stored in
current[]. The correct output appears when i have something like 8
minutes for example (actually, any single digit). When i have a double
digit (> 9), it doesnt work. I should also mention that the most sign
bit of the byte just read is immaterial and doesnt do anything.
To display this value to the lcd, it has to be in ASCII (thats where
the 0x30 comes in). So for single digits that are less than 10, i have
no problem (ds1305 outputs in bcd). Its when i need to determine the
10's digit that i fail. This is also happening with other bytes but if
i get a soln for one i can change the others accordingly. Thanks Jack.

Apr 15 '06 #3
zion_zii wrote:
I apologize for being so vague.

current and hours are both int ( 8 - bit ints) arrays. The correct
output appears when i have single digits. Prior to what is shown above,
the data is gathered from a terminal and fed into a chip (ds1305),
where a few minuites later, it is read from the same chip and stored in
current[]. The correct output appears when i have something like 8
minutes for example (actually, any single digit). When i have a double
digit (> 9), it doesnt work. I should also mention that the most sign
bit of the byte just read is immaterial and doesnt do anything.
To display this value to the lcd, it has to be in ASCII (thats where
the 0x30 comes in). So for single digits that are less than 10, i have
no problem (ds1305 outputs in bcd). Its when i need to determine the
10's digit that i fail. This is also happening with other bytes but if
i get a soln for one i can change the others accordingly. Thanks Jack.

The request way for sample data (since your code does not work)

//Looks like packed BCD No?
// 0x30 = '0' it reads better
//current[2] stores the byte i want to convert,
hours[0] = '0' + (current[2] >> 4); //obtain the tens minute digit
hours[1] = '0' + (current[2] & 0x0F) //obtain the ones minute digit.
Apr 15 '06 #4
Never mind guys. I figured it out. Just divide the num by 10 to get the
tens and subtract the tens * 10 from the initial val to get the ones.
Thanks though.

zion.

Apr 15 '06 #5
zion_zii wrote:
Never mind guys. I figured it out. Just divide the num by 10 to get the
tens and subtract the tens * 10 from the initial val to get the ones.
Thanks though.

Earlier you said that the data was obtained from a chip in BCD. Are you
now saying that the number has already been converted to binary, which
you didn't realize before, before your code converts it to ASCII
decimal? Otherwise, your explanation doesn't make sense.

--