Hello Perlmonks,

From an encrypted string I need to decode a 2-byte part, 16 bit. The 16 bit represent a date (year, month, day). The decoding is done using Crypt::DES. The 2-byte part is extracted with $packed_d = substr($packed_d,0,2); I then need the first 7 bits converted into a year, 4 bits for the month, and 5 bits for the day.

My code for selection of the bits does not work. The bitwise-& with my hex and packed_d always returns 0x00. In the example I construct a packed_d that I found in the data. Can someone tell me why "$packed_d & 0xFE00" always returns 0x00 ? (I replaced 0xFE00 with 0xFFFF and it still returns 0x00)

Thank you.

# raw contains the decrypted data my ($p1,$p1,$p3,$packed_d)=split(/\000/,$raw,4); $packed_d = substr($packed_d,0,2); #cut padding # temporary construction of packed_d my( $packed_d ) = pack( 'H4', d262 ); my $y = (($packed_d & 0xFE00) >> 9) + 4; my $m = (($packed_d & 0x01E0) >> 5) - 1; my $d = ($packed_d & 0x001F);

In reply to selecting and converting a bitstring by momo33

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.