从C ++的zlib到C#(如何的byte []转换成流和流为byte []) [英] zlib from C++ to C#(How to convert byte[] to stream and stream to byte[])

查看:246
本文介绍了从C ++的zlib到C#(如何的byte []转换成流和流为byte [])的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是使用zlib的解压缩(接收)数据包,然后使用algoritm从数据使图片

My task is to decompress a packet(received) using zlib and then use an algoritm to make a picture from the data

好消息是,我有代码在C ++中,但任务是做在C#

The good news is that I have the code in C++,but the task is to do it in C#

C ++

		//Read the first values of the packet received

		DWORD image[200 * 64] = {0}; //used for algoritm(width always = 200 and height always == 64)
		int imgIndex = 0; //used for algoritm
		unsigned char rawbytes_[131072] = {0}; //read below
		unsigned char * rawbytes = rawbytes_; //destrination parameter for decompression(ptr)
		compressed = r.Read<WORD>(); //the length of the compressed bytes(picture)
		uncompressed = r.Read<WORD>(); //the length that should be after decompression
		width = r.Read<WORD>(); //the width of the picture
		height = r.Read<WORD>(); //the height of the picture

		LPBYTE ptr = r.GetCurrentStream(); //the bytes(file that must be decompressed)

		outLen = uncompressed; //copy the len into another variable

		//Decompress

		if(uncompress((Bytef*)rawbytes, &outLen, ptr, compressed) != Z_OK)
		{
			printf("Could not uncompress the image code.\n");
			Disconnect();
			return;
		}

		//Algoritm to make up the picture
		// Loop through the data
		for(int c = 0; c < (int)height; ++c)
		{
			for(int r = 0; r < (int)width; ++r)
			{
				imgIndex = (height - 1 - c) * width + r;
				image[imgIndex] = 0xFF000000;
				if(-((1 << (0xFF & (r & 0x80000007))) & rawbytes[((c * width + r) >> 3)])) 
					image[imgIndex] = 0xFFFFFFFF;
			}
		}



我试图用zlib.NET做到这一点,但所有演示有代码解压缩(C#)

I'm trying to do this with zlib.NET ,but all demos have that code to decompress(C#)

    private void decompressFile(string inFile, string outFile)
	{
		System.IO.FileStream outFileStream = new System.IO.FileStream(outFile, System.IO.FileMode.Create);
		zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outFileStream);
		System.IO.FileStream inFileStream = new System.IO.FileStream(inFile, System.IO.FileMode.Open);			
		try
		{
			CopyStream(inFileStream, outZStream);
		}
		finally
		{
			outZStream.Close();
			outFileStream.Close();
			inFileStream.Close();
		}
	}

	public static void CopyStream(System.IO.Stream input, System.IO.Stream output)
	{
		byte[] buffer = new byte[2000];
		int len;
		while ((len = input.Read(buffer, 0, 2000)) > 0)
		{
			output.Write(buffer, 0, len);
		}
		output.Flush();
	}



我的问题:我不希望保存解压后的文件,因为我必须使用在C ++代码所示algoritm。

My problem:I don't want to save the file after decompression,because I have to use the algoritm shown in the C++ code.

如何将byte []数组转换成流类同一个在C#zlib的代码来解压数据,然后如何将流转换回字节数组?

How to convert the byte[] array into a stream similiar to the one in the C# zlib code to decompress the data and then how to convert the stream back into byte array?

另外,如何改变zlib.NET代码无法保存文件?

Also,How to change the zlib.NET code to NOT save files?

推荐答案

只需使用MemoryStreams,而不是文件流:

Just use MemoryStreams instead of FileStreams:

// Assuming inputData is a byte[]
MemoryStream input = new MemoryStream(inputData);
MemoryStream output = new MemoryStream();



然后你可以使用 output.ToArray()事后得到一个字节数组了。

Then you can use output.ToArray() afterwards to get a byte array out.

请注意,这是通常最好使用 语句,而不是一个单一的try / finally块 - 否则,如果到第一次调用关闭失败,其余的将不能进行。您可以嵌套他们是这样的:使用

Note that it's generally better to use using statements instead of a single try/finally block - as otherwise if the first call to Close fails, the rest won't be made. You can nest them like this:

using (MemoryStream output = new MemoryStream())
using (Stream outZStream = new zlib.ZOutputStream(output))
using (Stream input = new MemoryStream(bytes))
{
    CopyStream(inFileStream, outZStream);
    return output.ToArray();
}

这篇关于从C ++的zlib到C#(如何的byte []转换成流和流为byte [])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆