I have some C code which performs varbyte compression on ints. I have inline'd this code, and wish to manage the output and input with some perl. I know that the varbyte works in other contexts, but when i compare the length of the output to the length of the input, I guess that something is clearly wrong. I have no experience with binary data in perl, i just assumed that if i didnt touch it, it'd remain as is. here is the C which i know works properly in other contexts:
#include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct vbyte_record{ unsigned char* vbyte_result; short vbyte_len; } oneRecord; void displayBits(unsigned char*, int); oneRecord vbyte_compress(int); /*display bits of an unsigned char value*/ void displayBits(unsigned char* value, int len){ int shift = 7; unsigned char mask = '1' << shift; unsigned i, j; for(j=0; j<len; j++){ for(i=1; i<=8; i++){ printf("%c", value[j] & mask ? '1': '0'); value[j] <<= 1; } } return; } /* compress an integer into vbyte; first check how many bytes this integer needs; then set the highest bit of all byte to 1 except the lowest byte; encode each byte respectively */ oneRecord vbyte_compress(int number){ short index = (short) floor(log10(number)/log10(128)); //number of +bytes needed unsigned char* result; short i; int remainder = number; div_t temp; unsigned char mask = (char) 1 << 7; //used to set highest bit to 1 oneRecord record; result = (unsigned char*) malloc(sizeof(char)*(index+1)); /* if there are more than one byte; encode the higher byte */ if(index > 0){ for(i=index; i>=1; i--){ temp = div(remainder, (int)pow(128, i)); result[index-i] = (char) temp.quot | mask; remainder = temp.rem; } } /*encode the lowest byte*/ result[index] = (char) remainder; record.vbyte_result = result; record.vbyte_len = index+1; return record; } char* varbyte(int number) { oneRecord record; int decom_num; record = vbyte_compress(number); return record.vbyte_result; }
this line in my perl gives surprising results:
$compressed.= varbyte($x); print "$x: ",length($x)," compressed ",length($compressed), "\n";

In reply to keeping binary data raw by downer

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.