EmbeddedRelated.com
Forums

bcd 16 bits to bin

Started by falcosms June 2, 2007
Hi

I need a code example for conversion of a 16 bits BCD data in BIN , in
C (ccs PIC)

[]s

Marcelo
This is not C but...
E.g
9A(bcd)
01(9) 1010(A) (bin)
(16x9) + (1xA) = 154(dec)

Just keep in mind the weight of each bcd digit in a binary number is
16...
So for a 2-digit BCD number you simply multiply the MSD by 16, and then
add the LSD and you have the binary.

OK?
falcosms wrote:

> Hi
>
> I need a code example for conversion of a 16 bits BCD data in BIN , in
>
> C (ccs PIC)
>
> []s
>
> Marcelo
>
--
*******************************************
VISIT MY HOME PAGE:

LAST UPDATED: 23/08/2003
*******************************************
Regards
Eirik Karlsen
Ok!

But if my number is a 16 bits value?

Example= 1320 (bcd) = 528 (hex) --- How i do the conversion??
To: p...From: e...@online.noDate: Sun, 3 Jun 2007 11:52:03 +0200Subject: Re: [piclist] bcd 16 bits to bin

This is not C but... E.g 9A(bcd) 01(9) 1010(A) (bin) (16x9) + (1xA) = 154(dec)
Just keep in mind the weight of each bcd digit in a binary number is 16... So for a 2-digit BCD number you simply multiply the MSD by 16, and then add the LSD and you have the binary.
OK?
falcosms wrote:

Hi
I need a code example for conversion of a 16 bits BCD data in BIN , in C (ccs PIC)
[]s
Marcelo-- ******************************************* VISIT MY HOME PAGE: LAST UPDATED: 23/08/2003 ******************************************* Regards Eirik Karlsen

_________________________________________________________________
Hotmail to go? Get your Hotmail, news, sports and much more! Check out the New MSN Mobile!
http://mobile.msn.com
"Just keep in mind the weight of each bcd digit in a binary number is
16... "

So...
(16x16x5) + (16x1x2) + (1x8) = 1320(dec)

And for 3C7E(hex), vhich is 15486(dec):
(16x16x16x3) + (16x16xC) + (16x1x7) + (1xE) = 15486(dec)
12288 + 3072 + 112 + 14 = 15486(dec)

Keep in mind that mul by 16 can be done by:
-Rotate Left 4 times
-Loading a nibble(4bits)
-Swapping nibbles, and merging / masking

For the digit multiplication you'll probably have to use a proper
multiplication routine,
minimum a 4by8 , with 12 or 16bit output.
Fagundes Elrica wrote:

> Ok!
>
> But if my number is a 16 bits value?
>
> Example= 1320 (bcd) = 528 (hex) --- How i do the conversion??
>
> -------------------------
> To: p...
> From: e...@online.no
> Date: Sun, 3 Jun 2007 11:52:03 +0200
> Subject: Re: [piclist] bcd 16 bits to bin
>
> This is not C but...
> E.g
> 9A(bcd)
> 01(9) 1010(A) (bin)
> (16x9) + (1xA) = 154(dec)
>
> Just keep in mind the weight of each bcd digit in a binary
> number is 16...
> So for a 2-digit BCD number you simply multiply the MSD by
> 16, and then add the LSD and you have the binary.
>
> OK?
> falcosms wrote:
>
> Hi
>
> I need a code example for conversion of a 16 bits
> BCD data in BIN , in
> C (ccs PIC)
>
> []s
>
> Marcelo
>
> --
> *******************************************
> VISIT MY HOME PAGE:
>
> LAST UPDATED: 23/08/2003
> *******************************************
> Regards
> Eirik Karlsen
>
> -----------------------------------
> Hotmail to go? Get your Hotmail, news, sports and much more! Check out
> the New MSN Mobile
>

--
*******************************************
VISIT MY HOME PAGE:

LAST UPDATED: 23/08/2003
*******************************************
Regards
Eirik Karlsen
Ok, but in the other side?

I get the number 1320 in BCD and i want to convert in bin (or hex). How i do that?

To: p...From: e...@online.noDate: Sun, 3 Jun 2007 18:07:58 +0200Subject: Re: [piclist] bcd 16 bits to bin"Just keep in mind the weight of each bcd digit in a binary number is 16... "
So... (16x16x5) + (16x1x2) + (1x8) = 1320(dec)
And for 3C7E(hex), vhich is 15486(dec): (16x16x16x3) + (16x16xC) + (16x1x7) + (1xE) = 15486(dec) 12288 + 3072 + 112 + 14 = 15486(dec)
Keep in mind that mul by 16 can be done by: -Rotate Left 4 times -Loading a nibble(4bits) -Swapping nibbles, and merging / masking
For the digit multiplication you'll probably have to use a proper multiplication routine, minimum a 4by8 , with 12 or 16bit output.
Fagundes Elrica wrote:

Ok!
But if my number is a 16 bits value?
Example= 1320 (bcd) = 528 (hex) --- How i do the conversion??
To: p... From: e...@online.no Date: Sun, 3 Jun 2007 11:52:03 +0200 Subject: Re: [piclist] bcd 16 bits to bin
This is not C but... E.g 9A(bcd) 01(9) 1010(A) (bin) (16x9) + (1xA) = 154(dec)
Just keep in mind the weight of each bcd digit in a binary number is 16... So for a 2-digit BCD number you simply multiply the MSD by 16, and then add the LSD and you have the binary.
OK?
falcosms wrote:

Hi
I need a code example for conversion of a 16 bits BCD data in BIN , in C (ccs PIC)
[]s
Marcelo-- ******************************************* VISIT MY HOME PAGE: LAST UPDATED: 23/08/2003 ******************************************* Regards Eirik Karlsen
Hotmail to go? Get your Hotmail, news, sports and much more! Check out the New MSN Mobile
-- ******************************************* VISIT MY HOME PAGE: LAST UPDATED: 23/08/2003 ******************************************* Regards Eirik Karlsen
_________________________________________________________________
Hotmail to go? Get your Hotmail, news, sports and much more! Check out the New MSN Mobile!
http://mobile.msn.com
Basic math, BCD has the representation in powers of 10, once you convert
to binary then you can make any representation you want, Hex is just
powers of 16 in each place.

DLC

Fagundes Elrica wrote:
> Ok!
>
> But if my number is a 16 bits value?
>
> Example= 1320 (bcd) = 528 (hex) --- How i do the conversion??
>
> To: p...
> From: e...@online.no
> Date: Sun, 3 Jun 2007 11:52:03 +0200
> Subject: Re: [piclist] bcd 16 bits to bin
>
> This is not C but...
> E.g
> 9A(bcd)
> 01(9) 1010(A) (bin)
> (16x9) + (1xA) = 154(dec)
>
> Just keep in mind the weight of each bcd digit in a binary number is
> 16...
> So for a 2-digit BCD number you simply multiply the MSD by 16, and
> then add the LSD and you have the binary.
>
> OK?
>
>
> falcosms wrote:
>
> Hi
>
> I need a code example for conversion of a 16 bits BCD data in
> BIN , in
> C (ccs PIC)
>
> []s
>
> Marcelo
>
> --
> *******************************************
> VISIT MY HOME PAGE:
> > >
> LAST UPDATED: 23/08/2003
> *******************************************
> Regards
> Eirik Karlsen
>
>
> Hotmail to go? Get your Hotmail, news, sports and much more! Check out
> the New MSN Mobile

--
-------------
Dennis Clark TTT Enterprises
www.techtoystoday.com
-------------
--- In p..., Fagundes Elrica wrote:
> Ok, but in the other side?
>
> I get the number 1320 in BCD and i want to convert in bin (or hex).
How i do that?
>

You have to be able to extract the digits, normalize them and multiply
by the appropriate power of ten. You have 16 bits for a 4 digit BCD
number. Each 4 bit nibble can represent 0..9.

Something like:

BIN = ((BCD & 0xF000) >> 12) * 1000 + ((BCD & 0x0F00) >> 8) * 100 +
((BCD & 0x00F0) >> 4) * 10 + (BCD & 0x000F)

You can see how each nibble is extracted, shifted over to normalize
the value to 0..9 and then multiplied by the appropriate power of ten.

Both BIN and BCD need to be unsigned 16 bit values.

Richard
Very simple, just multiply the digits by their weight and sum!
(1000x1) + (100x3) + (10x2) + (1x0) = 1320dec, or 0101 0010 1000(bin),
or 528(hex)

One thing that is important is to be clear on what radix numbers are,
and in what radix those numbers is presented!
In BIN-BCD and BCD-BIN conversions its VERY easy to get confused!
The above is the "pen & paper" method. It is simple to understand but
it is not the fastest.

Many years ago I sat down and devised a much better procedure, took me
several days.
It is a 2-digit BCD2BIN and with a little extra code it can convert any
number of digits
This one is simple (little code) and it is very fast.
I won't go into much detail but the procedure is somewhat 'tweaked' and
obscure so its
not blatantly obvious how or why it works...but it does work, have used
it in many programs.
This is PIC18 asm:
;************** BCD2BIN16 ****************************
;Converts 2digit BCD number in WREG to BIN in WREG
;Uses ASCIIB5:ASCIIB4 as TEMP1:TEMP0
BCD2BIN16 MOVFF WREG,ASCIIB5 ;Get BCDBYTE0 and store in TEMP
SWAPF WREG ;Get MSD digit into low nibble
ANDLW 0X0F ;and discard LSD. This is a DIV by16.
MOVFF WREG,ASCIIB4 ;preseve BCD so operate on TEMP
ADDWF ASCIIB4 ;MUL BY 6
ADDWF ASCIIB4 ;
ADDWF ASCIIB4 ;
ADDWF ASCIIB4 ;
ADDWF ASCIIB4 ;
BSF STATUS,C ;Subtract result from BCD
MOVFF ASCIIB4,WREG ;and store in BCD
SUBWFB ASCIIB5,W ;WREG is now converted from BCD to BIN
RETURN
;*********************************************************

Since your BCD number has 4 digits you must run the routine on both
bytes, and multiply the
upper byte result by 100d before summing.
Like this

Upper byte:
13(bcd) [convert to BIN] = 0000 1101(bin), which is 0D(hex). Mul by
100(dec) = 10100010100(bin), which is 514(hex)

Lower byte:
20(bcd) [convert to BIN] = 0010 0000(bin), which is 14(hex)

Sum:
514(hex) + 14(hex) = 528(hex)
It figures??

E.K

Fagundes Elrica wrote:

> Ok, but in the other side?
>
> I get the number 1320 in BCD and i want to convert in bin (or hex).
> How i do that?
>
> -------------------------
> To: p...
> From: e...@online.no
> Date: Sun, 3 Jun 2007 18:07:58 +0200
> Subject: Re: [piclist] bcd 16 bits to bin
>
> "Just keep in mind the weight of each bcd digit in a binary
> number is 16... " So...
> (16x16x5) + (16x1x2) + (1x8) = 1320(dec)
>
> And for 3C7E(hex), vhich is 15486(dec):
> (16x16x16x3) + (16x16xC) + (16x1x7) + (1xE) = 15486(dec)
> 12288 + 3072 + 112 + 14 = 15486(dec)
>
> Keep in mind that mul by 16 can be done by:
> -Rotate Left 4 times
> -Loading a nibble(4bits)
> -Swapping nibbles, and merging / masking
>
> For the digit multiplication you'll probably have to use a
> proper multiplication routine,
> minimum a 4by8 , with 12 or 16bit output.
> Fagundes Elrica wrote:
>
> Ok!
>
> But if my number is a 16 bits value?
>
> Example= 1320 (bcd) = 528 (hex) --- How i do the
> conversion??
>
> -----
> To: p...
> From: e...@online.no
> Date: Sun, 3 Jun 2007 11:52:03 +0200
> Subject: Re: [piclist] bcd 16 bits to
> bin
>
> This is not C but...
> E.g
> 9A(bcd)
> 01(9) 1010(A) (bin)
> (16x9) + (1xA) = 154(dec)
>
> Just keep in mind the weight of each bcd
> digit in a binary number is 16...
> So for a 2-digit BCD number you simply
> multiply the MSD by 16, and then add the
> LSD and you have the binary.
>
> OK?
> falcosms wrote:
>
> Hi
>
> I need a code example for
> conversion of a 16 bits BCD
> data in BIN , in
> C (ccs PIC)
>
> []s
--
*******************************************
VISIT MY HOME PAGE:

LAST UPDATED: 23/08/2003
*******************************************
Regards
Eirik Karlsen
ok, very thanks!!!!!

[]s

Marcelo
To: p...From: e...@online.noDate: Sun, 3 Jun 2007 23:39:26 +0200Subject: Re: [piclist] bcd 16 bits to bin

Very simple, just multiply the digits by their weight and sum! (1000x1) + (100x3) + (10x2) + (1x0) = 1320dec, or 0101 0010 1000(bin), or 528(hex)
One thing that is important is to be clear on what radix numbers are, and in what radix those numbers is presented! In BIN-BCD and BCD-BIN conversions its VERY easy to get confused!
The above is the "pen & paper" method. It is simple to understand but it is not the fastest.
Many years ago I sat down and devised a much better procedure, took me several days. It is a 2-digit BCD2BIN and with a little extra code it can convert any number of digits This one is simple (little code) and it is very fast. I won't go into much detail but the procedure is somewhat 'tweaked' and obscure so its not blatantly obvious how or why it works...but it does work, have used it in many programs. This is PIC18 asm: ;************** BCD2BIN16 **************************** ;Converts 2digit BCD number in WREG to BIN in WREG ;Uses ASCIIB5:ASCIIB4 as TEMP1:TEMP0 BCD2BIN16 MOVFF WREG,ASCIIB5 ;Get BCDBYTE0 and store in TEMP SWAPF WREG ;Get MSD digit into low nibble ANDLW 0X0F ;and discard LSD. This is a DIV by16. MOVFF WREG,ASCIIB4 ;preseve BCD so operate on TEMP ADDWF ASCIIB4 ;MUL BY 6 ADDWF ASCIIB4 ; ADDWF ASCIIB4 ; ADDWF ASCIIB4 ; ADDWF ASCIIB4 ; BSF STATUS,C ;Subtract result from BCD MOVFF ASCIIB4,WREG ;and store in BCD SUBWFB ASCIIB5,W ;WREG is now converted from BCD to BIN RETURN ;*********************************************************
Since your BCD number has 4 digits you must run the routine on both bytes, and multiply the upper byte result by 100d before summing. Like this
Upper byte: 13(bcd) [convert to BIN] = 0000 1101(bin), which is 0D(hex). Mul by 100(dec) = 10100010100(bin), which is 514(hex)
Lower byte: 20(bcd) [convert to BIN] = 0010 0000(bin), which is 14(hex)
Sum: 514(hex) + 14(hex) = 528(hex)
It figures??
E.K
Fagundes Elrica wrote:

Ok, but in the other side?
I get the number 1320 in BCD and i want to convert in bin (or hex). How i do that?
To: p... From: e...@online.no Date: Sun, 3 Jun 2007 18:07:58 +0200 Subject: Re: [piclist] bcd 16 bits to bin
"Just keep in mind the weight of each bcd digit in a binary number is 16... " So... (16x16x5) + (16x1x2) + (1x8) = 1320(dec)
And for 3C7E(hex), vhich is 15486(dec): (16x16x16x3) + (16x16xC) + (16x1x7) + (1xE) = 15486(dec) 12288 + 3072 + 112 + 14 = 15486(dec)
Keep in mind that mul by 16 can be done by: -Rotate Left 4 times -Loading a nibble(4bits) -Swapping nibbles, and merging / masking
For the digit multiplication you'll probably have to use a proper multiplication routine, minimum a 4by8 , with 12 or 16bit output.
Fagundes Elrica wrote:

Ok!
But if my number is a 16 bits value?
Example= 1320 (bcd) = 528 (hex) --- How i do the conversion??
To: p... From: e...@online.no Date: Sun, 3 Jun 2007 11:52:03 +0200 Subject: Re: [piclist] bcd 16 bits to bin
This is not C but... E.g 9A(bcd) 01(9) 1010(A) (bin) (16x9) + (1xA) = 154(dec)
Just keep in mind the weight of each bcd digit in a binary number is 16... So for a 2-digit BCD number you simply multiply the MSD by 16, and then add the LSD and you have the binary.
OK?
falcosms wrote:

Hi
I need a code example for conversion of a 16 bits BCD data in BIN , in C (ccs PIC)
[]s -- ******************************************* VISIT MY HOME PAGE: LAST UPDATED: 23/08/2003 ******************************************* Regards Eirik Karlsen

_________________________________________________________________
With Windows Live Hotmail, you can personalize your inbox with your favorite color.
www.windowslive-hotmail.com/learnmore/personalize.html?locale=en-us&ocid=TXT_TAGLM_HMWL_reten_addcolor_0607
--- In p..., Fagundes Elrica
wrote:
> But if my number is a 16 bits value?
> Example= 1320 (bcd) = 528 (hex) --- How i do the conversion??

Thing is. A BCD number is already represented in Hex. So 1320 (bcd) -
is 0x1320. In decimal that would be 4896.

http://en.wikipedia.org/wiki/Binary-coded_decimal