473,774 Members | 2,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1898
On 14 Apr 2006 19:17:14 -0700, "zion_zii" <ni*******@gmai l.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.l earn.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.

--
Thad
Apr 16 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
7183
by: Westcoast Sheri | last post by:
What is the most efficient way of extracting the first two digits in a string? The following is wrong for me, because it only gives me the first instance of two digits together: $string = ujdk3ca94abc preg_match("/\d{2}/",$string,$result); echo "$result"; //prints 94. However, the result I am looking for is 39
4
12727
by: Anoop | last post by:
Hi All I am getting two different outputs when i do an operation using string.digits and test.isdigit(). Is there any difference between the two. I have given the sample program and the output Thanks for ur inputs Anoop
109
7642
by: jmcgill | last post by:
Hello. Is there a method for computing the number of digits, in a given numeric base, of N factorial, without actually computing the factorial? For example, 8! has 5 digits in base 10; 10! has 7 digits in base 10. Is there a way to compute that information without actually evaluating the factorial?
0
10267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9915
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6717
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4014
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2852
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.