[RFC] Basic compression scheme for char input.

End goal, compress user input (A-z) for a ratio of 2 characters to 1 byte. My strategy was to map user input to an array of acceptable character types, recording their index location within the charmap array, and finally storing the index location in decimal format. With a character map of 31 acceptable character types, possible character combinations can reach 1.25 bytes (0-30 dec <= 5bits ).

The problem I'm currently having... Getting an array of information in and out of dec2bin(). I had seen some examples that used pointers to pass the information but I could never get it to all gel together.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <cctype>
#include <cstring>
#include <cmath>

using namespace std;

/**
 * BICHARBYTE
**/

//Binary corresponds to decimal equivalent
int* dec2bin( int n[] )
{
        int remainder;
        int* pointer;
        int str_bin[sizeof(n)];
        pointer=str_bin;

        for( int i=0; i<sizeof(n); i++ ) {

                if( n[i]==0 || n[i]==1 ) {
                        //cout << n[i];
                        str_bin[i] = n[i];
                }

                remainder = n[i] % 2;
                n[i] >> 1;
                str_bin[i] = remainder;
        }

        return pointer;
}




void printarray( int arg[], int length )
{
        for( int n=0; n<length; n++ ) {
                cout << arg[n] << " ";
        }
        cout << "\n";
}



int main ( )
{
        const int MAX = 130;            //maximum number of characters allowed for input
        char str[MAX];                  //receiving array for all input

        //Reference for acceptable forms of input
        const char charmap[31] = {'a','b','c','d','e','f','g','h','i','j','k','l',
                                'm','n','o','p','q','r','s','t','u','v','w','x','y','z',
                                        '.',';','!','?',' ' };

        char ready='y';

        while( ready!='n' )
        {
                cout <<"Welcome!"<<endl;
                cout <<"Ready for input: ";
                cin.get(str, MAX);
                cin.ignore();
                char * pch;

                // Validates input by comparing each character against all of charmap
                int i=0;
                while( str[i] )
                {
                        pch = strchr(charmap, str[i]);
                        if( pch==NULL )
                        {
                                cout << "Invalid input. Try again? (y/n): ";
                                ready = cin.get();
                                cin.ignore();
                                break;
                        } else {
                                cout << "Please wait..." << endl;
                                ready = 'n';
                        }
                        i++;
                }
        }

        //Maps characters from str to charmap by index
        int char2int[MAX];
        int k=0;
        int i=0;
        int index=0;                    //Length of char2int
        while( str[k] ) {
                //MATCH FOUND! Record charmap[] index location to char2int[]
                if ( str[k] == charmap[i] ) {
                        char2int[index]=i;
                        index++;
                        k++;
                        i=0;
                // NO Match! Let's increment i in charmap[i] and reiterate through while
                } else {
                        i++;
                }
        }
 //Report
        cout << "<---------------- REPORT ---------------->" << endl;
        cout << "You entered: " << str << endl;
        char str_slice[strlen(str)];
        cout << "Size in memory: " << sizeof(str_slice) << " bytes" << endl;

        cout << "Character Map Indices: " << endl;
        printarray(char2int,strlen(str));

        //Converting int values of char2int to binary and printing/storing the results
        cout << "Binary Equivalent: " << endl;
        int char2int_size = index;
        int* str_bin = dec2bin(char2int);
        for( int i=0; i<char2int_size; i++ ) {
                cout << str_bin[i] << " ";
        }

        cout << endl;


return 0;
}





 > ./a.out
Welcome!
Ready for input: timbers kick ass
Please wait...
Please wait...
Please wait...
Please wait...
Please wait...
Please wait...
Please wait...
Please wait...
Please wait...
Please wait...
Please wait...
Please wait...
Please wait...
Please wait...
Please wait...
Please wait...
<---------------- REPORT ---------------->
You entered: timbers kick ass
Size in memory: 16 bytes
Character Map Indices:
19 8 12 1 4 17 18 30 10 8 2 10 30 0 18 18
Binary Equivalent:
1 16777216 603216 604232 1 17381448 603216 0 0 29358232 603216 16 -13161984 -14408724 -14379712 0

Last edited on
Topic archived. No new replies allowed.