[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1224
  • Last Modified:

Convert BCD to Integer

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
Chip Tanner
Asked:
Chip Tanner
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
Chip TannerAuthor Commented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now