简单的ASCII COM pression-有助于最大限度地减少系统调用 [英] Simple ASCII compression- Help minimize system calls

查看:103
本文介绍了简单的ASCII COM pression-有助于最大限度地减少系统调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的最后一个问题,NOS了去除的方法从ASCII字符字节,它描述了项目时匹配我的教授说正是最显著位。

In my last question, nos gave a method of removing the most significant bit from an ASCII character byte, which matches exactly what my professor said when describing the project.

我的问题是如何剥离显著位,并使用命令它打包到缓冲区中。因为命令接受一个长度的字节数写的,我怎么去更深的缓冲区数组的位水平?

My problem is how to strip the significant bit and pack it into a buffer using read and write commands. Since the write command takes in a length in the number of bytes to write, how do I go deeper to the bit level of the buffer array?

推荐答案

也许最简单的做到这一点的方法是在八个字节块。在阅读一大块那么COM preSS他们使用位运算符7个字节。

Probably the simplest way to do it is in chunks of eight bytes. Read in a chunk then compress them to seven bytes using bitwise operators.

让我们把输入数据输入[0..7] 和输出数据输出[0..6]

Let's call the input data input[0..7] and the output data output[0..6].

因此​​,输出数据的第一个字节,输出[0] ,由输入[0] 加上输入[2] 。这适用于所有其他人一样的:

So, the first byte of the output data, output[0], consists of the lower 7 bits of input[0] plus the second-most upper bit of input[2]. That works the same for all others:


    Index:    [0]      [1]      [2]      [3]      [4]      [5]      [6]      [7]
    Input:  0aaaaaaa 0bbbbbbb 0ccccccc 0ddddddd 0eeeeeee 0fffffff 0ggggggg 0hhhhhhh
            ///////  //////   and     --->
            ||||||| /|||||     so on  --->
    Output: aaaaaaab bbbbbbcc cccccddd ddddeeee eeefffff ffgggggg ghhhhhhh
    Index:    [0]      [1]      [2]      [3]      [4]      [5]      [6]

您可以使用操作,如:

output[0] = ((input[0] & 0x7f) << 1) | ((input[1] & 0x40) >> 6)
output[1] = ((input[1] & 0x3f) << 2) | ((input[2] & 0x60) >> 5)
:
output[5] = ((input[5] & 0x03) << 6) | ((input[6] & 0x7e) >> 1)
output[6] = ((input[6] & 0x01) << 7) |  (input[7] & 0x7f)

其他人应该从高于可计算的。如果您想了解更多关于位运算符,请参阅here.

一旦你的COM pressed一个八字节块,写出来的7字节的COM pressed块,继续向前。

Once you've compressed an eight-byte chunk, write out the seven-byte compressed chunk and keep going.

唯一稍微有点棘手是在最后,你可能不会有一个完整的八个字节。在这种情况下,你将输出的字节数,你输入,但是最后一节将用零比特填充。

The only slightly tricky bit is at the end where you may not have a full eight bytes. In that case, you will output as many bytes as you input but the final one will be padded with zero bits.

和上DECOM pression,你做的正好相反。阅读的7个字节的块,用位运算符扩大并写出八个字节。你也可以告诉它位在仅仅在读到最后一节的大小,根据最终填充。

And, on decompression, you do the opposite. Read in chunks of seven bytes, expand using bitwise operators and write out eight bytes. You can also tell which bits are padding at the end based solely on the size of the last section read in.

这篇关于简单的ASCII COM pression-有助于最大限度地减少系统调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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