Thread Subject: BCD to Decimal Conversion

Subject: BCD to Decimal Conversion

From: Ben

Date: 6 Nov, 2008 02:41:02

Message: 1 of 1

I am reading a bunch of binary data and extracting a time array (5bytes). I store them in a Nx5 array of uint8 class.

The data if viewed in hex would look like this:
HHMM SSss ss (Day Hour Min Sec. sec)
so an example starting at 12:01:12.3456 (0.001 sec increments)
..... in Hex ...
12 01 12 34 56
12 01 12 34 66
12 01 12 34 76
.... in decimal ....
18 01 18 52 86
18 01 18 52 102
18 01 18 52 118
.......

I am currently "decoding" the BCD to its real decimal equivalent in the following way using bitshift and bitand. Then converting to seconds of the day
% T is time array (nx6) uint8
h = bitshift(T(:,1),-4)*10 + bitand(15,T(:,1));
m = bitshift(T(:,2),-4)*10 + bitand(15,T(:,2));
s = bitshift(T(:,3),-4)*10 + bitand(15,T(:,3)) + bitshift(T(:,4),-4)*.1 + bitand(15,T(:,4))*.01 + bitshift(T(:,5),-4)*.001 + bitand(15,T(:,5))*.0001 ;
TotalSec = h*3600+m*60+s;

So my question is ...Is there an easier way to go from BCD to Decimal.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
bitshift bitand... Ben 5 Nov, 2008 21:45:03
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com