Perl-Sensitive Sunglasses PerlMonks

### Binary Coded Decimal

 on Sep 17, 2012 at 23:53 UTC Need Help??
adrce55 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I need to convert a integer value (which will always be less than 99), into a binary coded decimal. it would have to be packed into one byte. Any ideas of where/how to start? Thanks

Replies are listed 'Best First'.
Re: Binary Coded Decimal
by Anonymous Monk on Sep 18, 2012 at 01:44 UTC
perl pack bcd -> BCD Clock built using only binary logic., Encoding BCD

so

```sub encode_BCD {
return pack 'H*', join '', @_
}

If you need to ensure < 99 , or dealing with signs properly, you'll have to add that part

Seems to work https://en.wikipedia.org/wiki/Binary_coded_decimal

```-1,234,56
0001 0010 0011 0100 0101 0110 0111 1101
1    2    3    4    5    6    7    -

\$ perl -le " print join q/ /, split /....\K/, unpack q/B*/, pack q/H*/
+, q/1234567-/ "
0001 0010 0011 0100 0101 0110 0111 1101

\$ perl -le " print join q/ /, unpack q/(A4)*/, unpack q/B*/, pack q/H*
+/, q/1234567-/ "
0001 0010 0011 0100 0101 0110 0111 1101

Re: Binary Coded Decimal
by Marshall (Abbot) on Sep 18, 2012 at 07:00 UTC
BCD, Binary Coded Decimal is a system where each 4 binary bits represent a decimal digit. The hexadecimal digits A-F are not used in this representation.

This format is a bit odd nowadays (more normal would be ASCII decimal which takes 2 8 bit bytes instead of 2 4 bit nibbles, but that extra space is usually not of consequence). Anyway this is how to calculate the "digits". The integer "div" or modulo operation is only one machine instruction and the integer multiply is also only one machine instruction. I don't know how good Perl is at this. See Perl pack() and unpack() for how to cram these into one 8 bit value.

```#!usr/bin/perl -w
use strict;

my @tests = (2,11,23,89,99);

foreach my \$num (@tests)
{
my \$units = \$num % 10; #modulo is the remainder
my \$tens = (\$num - \$units)/10;
printf "decimal=%02d BCD=%04b %04b\n", \$num, \$tens, \$units;
}

__END__
decimal=02 BCD=0000 0010
decimal=11 BCD=0001 0001
decimal=23 BCD=0010 0011
decimal=89 BCD=1000 1001
decimal=99 BCD=1001 1001
Re: Binary Coded Decimal
by tobyink (Abbot) on Sep 18, 2012 at 00:58 UTC
```sub bcd
{
local \$_ = shift;

# enforce two digits
\$_ = "0\$_" while 2 > length;
die "too big" unless m{^(\d)(\d)\$};

return chr(16*\$1 + \$2);
}
perl -E'sub Monkey::do{say\$_,for@_,do{(\$monkey=[caller(0)]->[3])=~s{::}{ }and\$monkey}}"Monkey say"->Monkey::do'
Re: Binary Coded Decimal
by NetWallah (Canon) on Sep 18, 2012 at 00:00 UTC
perlebcdic is a great place to start on this quest.

I hope life isn't a big joke, because I don't get it.
-SNL

I fail to see the connection, and how the linked document is useful in this question. Can you please explain it?

Sure, EBCDIC is historically based on BCD, but I don't see how that is useful with the conversion from numbers to BCD.

I figured the explanations were useful, and the conversion code in the article could serve as a staring-point example, and be modified to deal with BCD. The OP was looking for a starting point. I did not find any direct BCD starting points, so I posted the EBCDIC one.

I hope life isn't a big joke, because I don't get it.
-SNL

Re: Binary Coded Decimal
by DrHyde (Prior) on Sep 20, 2012 at 10:32 UTC

Create A New User
Node Status?
node history
Node Type: perlquestion [id://994154]
Approved by ww
help
Chatterbox?
 Eily the verb from this sentence [LanX]: jdporter adjusted the best nodes algorithm not long ago to public demands, my theory more complicated= more error prone [Eily]: what error? I see no issue :P LanX and: = harder to reproduce. .. [LanX]: sorry, i didn't break math yet, but it's top priority on my todo list

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (11)
As of 2018-03-20 10:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (249 votes). Check out past polls.

Notices?