Int 和 *Char 数组 - C++

发布于 2024-10-07 14:15:08 字数 455 浏览 0 评论 0原文

我想使用 LZO 来压缩 int 或 byte 数组。所以我需要将 int 数组复制到 *char 然后我将压缩并保存到文件。然后我需要做反向操作。我将打开文件,用 *Char 读取它,并将其解压缩为 int 数组。

我不想查看 *char 来转换每个 int。有什么方法可以快速做到这一点吗?

char *entrada;
int *arrayInt2;
int arrayInt1[100];
int ctr;

for(ctr=0;ctr<=100; ctr++)
{
    arrayInt1[ctr] = ctr;
} 

entrada = reinterpret_cast<char *>(arrayInt1);
arrayInt2 = reinterpret_cast<int *>(entrada);

return 0;

我想要这样的东西。这是正确的吗? 谢谢

I want to use LZO to compress a array of int or byte. So I need to copy the int array to a *char then I will compress and save to file. And after i need do reverse operation. I will open the file read it with *Char and the decompress to array of int.

I don't want to do a look in the *char to convert each int. Is the any way to do this quickily?

char *entrada;
int *arrayInt2;
int arrayInt1[100];
int ctr;

for(ctr=0;ctr<=100; ctr++)
{
    arrayInt1[ctr] = ctr;
} 

entrada = reinterpret_cast<char *>(arrayInt1);
arrayInt2 = reinterpret_cast<int *>(entrada);

return 0;

I want something like this. Is this correct?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月染过的梦 2024-10-14 14:15:08

您可以将整数数组直接视为(二进制)字符缓冲区并将其传递给压缩函数:

char *buffer = reinterpret_cast<char *>(my_int_array);

同样,当您解压缩到字符缓冲区时,您可以将其用作整数数组:

int *array = reinterpret_cast<int *>(my_char_buffer);

确保跟踪原始数据整数数组的长度并且您不会访问无效索引。

You can treat the integer array directly as a (binary) character buffer and pass it to your compression function:

char *buffer = reinterpret_cast<char *>(my_int_array);

And similarly when you decompress into a character buffer, you can use it as an integer array:

int *array = reinterpret_cast<int *>(my_char_buffer);

Make sure that you keep track of the original length of the integer array and that you don't access invalid indices.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文