fork download
  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4. char s[]="helloo";
  5.  
  6. unsigned char last=0; // remaining bits from previous iteration in high output part
  7. size_t j=5; // number of high input bits to keep in the low output part
  8. unsigned char output=0;
  9. for (char *p=s; *p; p++) { // iterate on the string
  10. do {
  11. output = ((*p >> (8-j)) | last) & 0x1f; // last high bits set followed by j bits shifted to lower part; only 5 bits are kept
  12. printf ("%02x ",(unsigned)output);
  13. j += 5; // take next block
  14. last = (*p << (j%8)) & 0x1f; // keep the ignored bits for next iteration
  15. } while (j<8); // loop if second block to be extracted from current byte
  16. j -= 8;
  17. }
  18. if (j) // there are trailing bits to be output
  19. printf("%02x\n",(unsigned)last);
  20.  
  21. return 0;
  22. }
  23.  
Success #stdin #stdout 0s 2168KB
stdin
Standard input is empty
stdout
0d 01 12 16 18 1b 03 0f 0d 1c