473,385 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

BCD-Decimal

I am trying to figure out how to convert a BCD number to its decimal
equivilant.
Jul 18 '07 #1
4 26814
"Stephen" <no****@nospam.nowrote in message
news:ey**************@TK2MSFTNGP06.phx.gbl...
>I am trying to figure out how to convert a BCD number to its decimal
equivilant.
One way (not necesarily optimal) is this:

using System;

class Test
{
public static void Main()
{
byte[] bcdNumber = { 0x12, 0x34, 0x56, 0x78 };

long result=0;
foreach (byte b in bcdNumber)
{
int digit1 = b >4;
int digit2 = b & 0x0f;
result = (result * 100) + digit1 * 10 + digit2;
}
Console.WriteLine("{0}", result); //12345678
}
}
Jul 18 '07 #2
What I was really looking for was a solution that would allow for a binary
number encoded in BCD to be converted to decimal. Eg

10000 11001 = 1 3

Could do a switch statement but thought there might be a better solution.

"Alberto Poblacion" <ea******************************@poblacion.orgwro te
in message news:ex**************@TK2MSFTNGP05.phx.gbl...
"Stephen" <no****@nospam.nowrote in message
news:ey**************@TK2MSFTNGP06.phx.gbl...
>>I am trying to figure out how to convert a BCD number to its decimal
equivilant.

One way (not necesarily optimal) is this:

using System;

class Test
{
public static void Main()
{
byte[] bcdNumber = { 0x12, 0x34, 0x56, 0x78 };

long result=0;
foreach (byte b in bcdNumber)
{
int digit1 = b >4;
int digit2 = b & 0x0f;
result = (result * 100) + digit1 * 10 + digit2;
}
Console.WriteLine("{0}", result); //12345678
}
}



Jul 18 '07 #3
Stephen <no****@nospam.nowrote:
What I was really looking for was a solution that would allow for a binary
number encoded in BCD to be converted to decimal. Eg

10000 11001 = 1 3
How does 10000 represent 1 and 11001 represent 3 in this case? Did you
deliberately put each of the digits in 5 bits rather than 4?
Could do a switch statement but thought there might be a better
solution.
If we understood the BCD variant you're using better, I'm sure we could
come up with something.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 18 '07 #4
On Wed, 18 Jul 2007 14:10:10 -0400, "Stephen" <no****@nospam.no>
wrote:
>What I was really looking for was a solution that would allow for a binary
number encoded in BCD to be converted to decimal. Eg

10000 11001 = 1 3
Do you mean 0001 0011 -1 3?

How is your BCD number held, as a byte array or what?

How do you want the decimal equivalent, as a string, int, long, double
or what?

Here is some example code to convert BCD held in a big-endian byte
array to a long:

long BCD2Long(byte source) {
long result = 0;
foreach (byte b in source) {
int hi = b >4;
int lo = b & 0x0F;
result *= 10;
result += hi;
result *= 10;
result += lo;
}
return result;
}

Warning, neither tested nor compiled. Caveat emptor.

rossum

Jul 18 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: George | last post by:
Does anyone know where to find an algorithm that mulitplies and divides two BCD numbers. Thanks a lot for the help.
10
by: August Derleth | last post by:
Cause I made such a hash of my first attempt, and in an effort to provide PORTABLE code to solve the problem, I submit this solution to convert to and from BCD values stored in longs. This should...
12
by: micmic | last post by:
Dear all experts, I have the Coding as following: In File abc.cs namespace A { public class abc { public abc() {
67
by: Philippe Martin | last post by:
Hi, I'm looking for an algo that would convert a list such as: I'm using python to prototype the algo: this will move to C in an embedded system where an int has 16 bits - I do not wish to use...
0
by: yxq | last post by:
Hello The new wmi BCD class in Vista. http://msdn2.microsoft.com/en-gb/library/aa362663.aspx Who can tell how to invoke the BcdObject class using vb.net? thank you.
5
by: RobinS | last post by:
I want to serialize a class that I am using to retain some information the user types into a screen. I have 3 questions. 1) I serialized it as XML to start with. This works, but how do I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.