Jump to content

[Linux] Convert and then convert again and ...

- - - - -

  • Please log in to reply
2 replies to this topic

#1
sakishrist

sakishrist

    Programmer

  • Members
  • PipPipPipPip
  • 109 posts
Hello everybody,

I am having a problem (or lots of that type) finding a working way to convert some char(s) to Bytef type (used by the zlib).

I am kind of new to C++ programing and syntax so I might be missing something obvious.

Anyhow, here is my program:
#include <iostream>
#include <zlib.h>

using namespace std;

void _compress(unsigned char & in_data, size_t in_size, unsigned char & out_data, size_t out_size){

z_stream z;

int status;

    z.avail_in = 0;
    z.next_out = &out_data;
    z.avail_out = out_size;

        if ( z.avail_in == 0 ) {
            z.next_in = &in_data;
            z.avail_in = in_size; //dfjngdfng
        }
        if ( z.avail_in != 0 )
            status = deflate( &z, Z_NO_FLUSH );
        int count = out_size - z.avail_out;

} 



int main(){
    unsigned char a[100];//13
    a=reinterpret_cast<unsigned char>("Hello world!");
    unsigned char b[100];
    _compress(*b,13,*b,100);
    cout << b << endl;
    return 0;
}
My goal is to make an easy-to-use function that will compress (and later de-compress) a buffer.

The error I get with this last compilation attempt is:
sakishrist@sakishrist-laptop:~/Desktop/c++/zlib$ g++ zlib.cpp -o  zlib_c -lz
zlib.cpp: In function ‘int main()’:
zlib.cpp:42:50: error: cast from ‘const char*’ to ‘unsigned char’ loses precision
zlib.cpp:42:50: error: incompatible types in assignment of ‘unsigned char’ to ‘unsigned char [100]’
I tried many different things but all of them fail. I think (I can be wrong with so many errors) that I know how to use * and & in general.

I hope you can give me a piece of advice. :)

Thanks in advance!!!

#2
jakash3

jakash3

    Newbie

  • Members
  • PipPip
  • 21 posts
You can't really assign a raw literal string to a character array. If you wanted to set the contents of a to "hello world" you could use strcpy(a, "hello world"); (after #include <string.h>).

I'm guessing _compress is supposed to compress a block of data somehow. In that case the 1st argument should be unsigned char * in_data (as goes for out_data too); and call the function without adding a * or & in front of a.
Posted Image
Posted Image

#3
sakishrist

sakishrist

    Programmer

  • Members
  • PipPipPipPip
  • 109 posts
Thank you very much for the reply,

After making the String-to-Char conversion work as intended and making the changes you suggested I get e compilation error:
sakishrist@sakishrist-laptop:~/Desktop/c++/zlib$ g++ zlib.cpp -o zlib_c -lz
zlib.cpp: In function ‘void _compress(unsigned char*, size_t, unsigned char*, size_t)’:
zlib.cpp:13:16: error: cannot convert ‘unsigned char**’ to ‘Bytef*’ in assignment
zlib.cpp:17:17: error: cannot convert ‘unsigned char**’ to ‘Bytef*’ in assignment

If, on the other hand, I leave the last part out, so that my code looks like this:
#include <iostream>
#include <zlib.h>

using namespace std;

void _compress([B][COLOR=red]unsigned char & in_data[/COLOR][/B], size_t in_size, [B][COLOR=red]unsigned char & out_data[/COLOR][/B], size_t out_size){

z_stream z;

int status;

    z.avail_in = 0;
    z.next_out = &out_data;
    z.avail_out = out_size;

        if ( z.avail_in == 0 ) {
            z.next_in = &in_data;
            z.avail_in = in_size; //dfjngdfng
        }
        if ( z.avail_in != 0 )
            status = deflate( &z, Z_NO_FLUSH );
        int count = out_size - z.avail_out;

} 



int main(){
    unsigned char a[100]="Hello world";    [COLOR=blue]//Although this is not what you suggested I think it works fine but, please correct me if I am wrong.[/COLOR]
    //a=reinterpret_cast<unsigned char>("Hello world!");
    unsigned char b[100];
    _compress([COLOR=red][B]*a[/B][/COLOR],13,[B][COLOR=red]*b[/COLOR][/B],100); 
    cout << b << endl;
    return 0;
}


Then it compiles OK but I get a runtime error: "Segmentation Fault"

I guess the program tries to access memory that it does not have but ... I don't quite get it. :crying:

Thanks again.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users