Hi, I have a real-world problem encoding and decoding 7- & 8-bit values. The context is a MIDI SysEx file. In MIDI, the high bit of a byte is used to signal end-of-file, so all the data bytes must be 7 bits. When sending 8-bit data, my synth (a Korg Karma, if you care) encodes eight-bit data as follows:
+--------------------------------------------------------- | Internal 7byte data <--convert--> MIDI 8 byte data + | | example: Internal data(bit image) MIDI data(bit image) + | | Aaaaaaaa 0GFEDCBA + | | Bbbbbbbb 0aaaaaaa + | | Cccccccc 0bbbbbbb + | | Dddddddd 0ccccccc + | | Eeeeeeee 0ddddddd + | | Ffffffff 0eeeeeee + | | Gggggggg 0fffffff + | | Hhhhhhhh 0ggggggg + | | Iiiiiiii 0NMLKJIH + | | : 0hhhhhhh + | | : : + | | Vvvvvvvv 000000WV + | | Wwwwwwww 0vvvvvvv + | | 0wwwwwww + | | 11110111 (EOX=F7H) + | +---------------------------------------------------------
Now, I wish to decode the 7-bit data to 8-bit data, and back again. The question is, what's the most efficient approach? I'm sure something using unpack & vec should be in order, but I'm unsure which is the most (time-)efficient approach. I'm gonna be doing this a fair bit, so I'm willing to trade readability for speed. Ideas gratefully received, Thanks.

In reply to Converting 7 & 8-bit values by fluffy

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.