Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Binary Coded Decimal

by adrce55 (Initiate)
on Sep 17, 2012 at 23:53 UTC ( #994154=perlquestion: print w/replies, xml ) 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
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.

      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.

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://994154]
Approved by ww
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2018-10-19 16:35 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (110 votes). Check out past polls.