Solved

Convert BCD to Integer

Posted on 2006-10-27
2
1,098 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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