Solved

Convert BCD to Integer

Posted on 2006-10-27
2
1,059 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

943 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

11 Experts available now in Live!

Get 1:1 Help Now