triumphost 120 Posting Whiz

I'm trying to convert this (Pascal) to C++.. My attempt is below this..

function CompressString(const Str: string): string;
var
  Destlen:longword;
begin
  result := '';
  Destlen :=BufferLen;
  if length(str) < 1 then
    exit;
  if compress(BufferString,destlen,PChar(Str),length(str)) = Z_OK then
  begin
    setlength(result,Destlen + SizeOf(Integer));
    PInteger(@result[1])^ := Length(str);
    Move(bufferstring[0],result[5],Destlen);
  end;
end;
string CompressString(string Source)
{
    unsigned long dsize;                    //Compressed Datasize
    size_t SourceSize = Source.size();
    size_t BufferLen = SourceSize + (SourceSize * 0.1) + 12;        //Must have a min buffer size = to the source + itself * 0.1 + 12
    char dest[BufferLen];

    if (SourceSize < 1)
        return dest;

    //Compress everything from source to destination
    if (compress((unsigned char *)dest, &dsize, (const unsigned char *)Source.c_str(), SourceSize) != Z_OK)
        cout<<"Compression Error!\n";

    return dest;
}

But they both print different results.. any idea why?

Dani AI

Generated

— two concrete areas to verify so the C++ output matches the Pascal output.

First, zlib usage: the compress call requires the caller to pass the available output buffer size by value; zlib then writes the actual compressed length back into that parameter. If that "available size" parameter is not set before the call, compress can fail or produce an unexpected result. Also check the compress return code and avoid continuing when it is not Z_OK. See the zlib docs for the exact contract: zlib compress docs.

Second, binary versus text handling and the returned layout. Compressed data is binary and can contain NUL bytes. Converting a raw char* into an std::string via the NUL-terminated constructor will truncate at the first zero. Build the std::string with an explicit length (pointer + length) so embedded zeros are preserved. If the Pascal function adds a 4‑byte original-length header, either add the same header in C++ (use a fixed-width type such as uint32_t and decide a byte order) or strip it when comparing results — mismatched headers will make the two outputs differ even if the compressed payload is identical.

Quick checklist to resolve the difference:

  • Initialize the output-size parameter to the buffer capacity before calling compress and check the return value.
  • Use a dynamic buffer (std::vector<unsigned char> or resized std::string) instead of a compiler-dependent VLA.
  • Construct the returned string with an explicit length: e.g., std::string(reinterpret_cast<char*>(buf.data()), outLen).
  • If compatibility with Pascal is required, write/read a fixed 4‑byte header (uint32_t) and agree on endianness (use htonl/ntohl if you need network order).

Follow these steps and compare lengths and a hex dump of the first few bytes from both implementations; that will quickly show whether the mismatch is a header/endianness issue or a compression/buffer bug.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.