Hello Monks,
I read the man pages for pack and unpack but must admit that I am a bit lost. Here is what I have:
$data = '00010003206162';
$kategory = hex(substr($data,0,4));
$length = hex(substr($data,4,4));
$contents = pack('H'.$length*2, substr($data, 8, length($data)));
So the structure is:
- 4 characters with the kategory (hex number, here: 1),
- 4 characters with the length of the contents (hex number, here: 3),
- the contents as hex data (here: '206162' = ' ab')
- more data in the same or similar format
Now this code has to be really fast and I am sure it can be optimized. Since pack/unpack are very fast I hope that it is possible to do it along these lines (not working):
my ($kat, $contents) = pack("h4 h4/H*", $data);
It should somehow go with help of '/' I found in the pack manpage but must admit that I didn't understand it and have no idea how the given examples work.
As an additional complication sometimes the size isn't given directly but has to be subtracted from 65536, e.g. a value of 'fff5' means 65536 - 65525 = 11 as the size. But even if this esoteric counting can not be directly done it would help a lot if I could improve at least the more simple case above.
Any idea how this is best done?
Thanks
Michael
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.