encode2 encodes a 4 integer value to 2 bytes bcd: 3002 -hex> 33.30.30.32 -bcd> 30.02 decode_2 takes 2 byte bcd and creates 4 bytes value: 30.02 -hex> 30.02 -acsii> 33.30.30.32 -value> 3002 sub's are not variable length "<func>2": 2 byte bcd,"<func>3": 3 byte bcd
sub encode_2 { $_=sprintf "%04d", shift; /(\d)(\d)(\d)(\d)/; return chr($1<<4|$2).chr($3<<4|$4); } sub encode_3 { $_=sprintf "%06d", shift; /(\d)(\d)(\d)(\d)(\d)(\d)/; return chr($1<<4|$2).chr($3<<4|$4).chr($5<<4|$6); } sub decode_2 { ($a,$c)=unpack("A" x 2, shift); return (ord($a)>>4)*1000+(ord($a)&0x0f)*100+(ord($c)>>4)*10+(ord($c) +&0x0f); } sub decode_3 { ($a,$b,$c)=unpack("A" x 3, shift); return (ord($a)>>4)*100000+(ord($a)&0x0f)*10000+(ord($b)>>4)*1000+(o +rd($b)&0x0f)*100+(ord($c)>>4)*10+(ord($c)&0x0f); }

In reply to bcd2ascii and reverse by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.