Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

packing into a byte token

by dulac (Initiate)
on Sep 21, 2011 at 00:28 UTC ( [id://927039]=perlquestion: print w/replies, xml ) Need Help??

dulac has asked for the wisdom of the Perl Monks concerning the following question:

Greetings.

I'm seeking wisdom from the monastery on handling creation of an array of bytes. I've done some research and it appears that I can use the pack command, but I am at a loss on how to obtain the results I seek.

I need to construct a "token" formatted as:

bytes 0 - 3: unsigned 32 bit representation of seconds since the epoch bytes 4 - 7: unsigned 32 bit number (this represents an employee id) byte 8: A check byte computed from the exclusive-or ("xor") of all the + previous bytes

I understand I can get the seconds since the epoch using time(), and I have the employee id as a 9 digit number - how do I format these into the above token and how do I create the xor check byte?

Any and all assistance is greatly appreciated.

Replies are listed 'Best First'.
Re: packing into a byte token
by BrowserUk (Patriarch) on Sep 21, 2011 at 01:06 UTC

    The checksum spec is a little vague. It could be:

    my $packed = pack 'NN', time(), 123456789;; my $c = chr(0); $c ^= substr $packed, $_, 1 for 0 .. 7;; $packed .= $c;; print unpack 'C*', $packed;; 78 121 55 23 7 91 205 21 147

    Or:

    my $packed = pack 'NN', time(), 123456789;; my $c = substr $packed, 0, 1; $c ^= substr $packed, $_, 1 for 1 .. 7;; $packed .= $c;; print unpack 'C*', $packed;; 78 121 66 151 7 91 205 21 102

    Probably the latter.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: packing into a byte token
by jwkrahn (Abbot) on Sep 21, 2011 at 00:41 UTC
    my $seconds_since_the_epoch = pack 'N', time; my $employee_id = pack 'N', 123456789; my $check_byte = $seconds_since_the_epoch ^ $employee_id;
      Need to xor each byte, so
      use List::Util qw( reduce ); my $token = pack 'NN', time, $employee_id; $token .= reduce { $a ^ $b } split //, $token;

      Use "V" instead of "N" if you need the opposite byte order.

        Need to xor each byte

        Because?

Re: packing into a byte token
by chrestomanci (Priest) on Sep 21, 2011 at 08:23 UTC

    There is an old but a great tutorial in a dusty corner here in the monastery on how to to use pack/unpack that google always finds if you search for perl pack/unpack documentation.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://927039]
Approved by koolgirl
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-24 08:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found