Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Convert BCD to Integer

Posted on 2006-10-27
2
Medium Priority
?
1,176 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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

636 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