BrowserUk:

A relatively simple C program to do the task:

roboticus@Boink~ $ cat 876421.cpp #include <stdio.h> unsigned char inbuf[]="Now is the time for all "; unsigned char outbuf[50]; void hexdump(unsigned char *p, int n) { for (int i=0; i<n; i++) { printf("%02x ", *p++); } puts("\n"); } int main(int, char **) { unsigned char *src = inbuf; unsigned char *srcEnd = src + sizeof(inbuf); unsigned char *dst = outbuf; hexdump(src, sizeof(inbuf)); int rem = 0; int st = 0; while (src != srcEnd) { switch (st) { case 0: rem = *src >> 6; *dst++ = *src++ & 0x3f; st = 1; break; case 1: *dst++ = ((*src&0x0f)<<2) | rem; rem = *src++>>4; st = 2; break; case 2: *dst++ = ((*src&0x03)<<4) | rem; *dst++ = *src++>>2; st = 0; break; } } hexdump(outbuf, dst-outbuf); } roboticus@Boink~marco@Boink:~ $ gcc 876421.cpp -lstdc++ roboticus@Boink:~ $ ./a.out 4e 6f 77 20 69 73 20 74 68 65 20 74 69 6d 65 20 66 6f 72 20 61 6c 6c 2 +0 00 0e 3d 36 1d 20 24 36 1c 20 10 07 1a 25 01 02 1d 29 35 16 19 20 18 36 1 +b 32 01 12 18 2c 31 06 08 00

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: unpacking 6-bit values by roboticus
in thread unpacking 6-bit values by BrowserUk

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.