Solved

Convert BCD to Integer

Posted on 2006-10-27
2
1,049 Views
Last Modified: 2012-05-05
I have a binary file that I am receiving from a device.  The data file that I receive has the following specifications for the first 18 bytes.  I am focusing on bytes 6-17 for the date information.

Offset      Length (bytes)      Contents
0      1      Start Page checksum
1      2      Session Index
3      2      Size of the monitor log
5      1      Out of memory
6      1      Start day - Stored in BCD format
7      1      Start month - Stored in BCD format
8      1      Start year - Stored in BCD format
9      1      Start second - Stored in BCD format
10      1      Start minute - Stored in BCD format
11      1      Start hour - Stored in BCD format
12      1      End day - Stored in BCD format
13      1      End month - Stored in BCD format
14      1      End year - Stored in BCD format
15      1      End second - Stored in BCD format
16      1      End minute - Stored in BCD format
17      1      End hour - Stored in BCD format

Here is the binary data.

010011000000001100000000011110010010000100000000001100000000000100000000010000110010011000000110001100000000000100000000010000000010011100000110

Here is the data in hex.

4C0300792100300100432606300100402706

I am looking for a simple routine to convert the BCD (Offsets 6 - 17) into the Integer values.  I have tried a few ways, but to no avail.  It may be something very simple that I am overlooking.

Thanks.

0
Comment
Question by:Chip Tanner
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 250 total points
ID: 17822379
I'm guessing you've managed to read the data in already. if that is the case then you can use the following to convert the binary value to its decimal equivalent:

        public int Bin2Dec(int binVal)
        {
            int remainder, value = 0;

            for (int i = 0; binVal != 0; i++)
            {
                remainder = binVal % 10;
                value = value + remainder * (int)Math.Pow((double)2, (double)i);
                binVal = binVal / 10;
            }

            return value;
        }
0
 

Author Comment

by:Chip Tanner
ID: 17825174
This works perfectly.  I have the data read in.  I was just having a problem with the BCD.  This worked very well.  Thanks.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now