469,290 Members | 1,941 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

BCD-Decimal

I am trying to figure out how to convert a BCD number to its decimal
equivilant.
Jul 18 '07 #1
4 24854
"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 discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.