Obtaining digits from bit string

Z

zion_zii

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.
 
J

Jack Klein

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?
 
Z

zion_zii

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.
 
N

Neil

zion_zii said:
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.
 
Z

zion_zii

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.
 
T

Thad Smith

zion_zii said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top