是否有更好的方法在两个不同类的两个字节缓冲区之间进行转换?

问题描述 投票:0回答:1

所以,我将 TagLib 库与 Godot 引擎结合使用,我想以字节数组的形式在这两者之间发送大量数据。问题是两者都有自己的变量来存储字节:

Godot:PoolByteArray 标签库:字节向量

要将 TagLib 函数中的数据返回到我的 Godot 项目中,我必须将 ByteVector 转换为 PoolByteArray。

我目前的做法是这样的:

godot::PoolByteArray ByteVector2PoolByte(TagLib::ByteVector data) {
godot::PoolByteArray x;
for (size_t i = 0; i < data.size(); i++) {
    x.push_back(data[i]);
}
return x;

这对我来说似乎非常低效,我认为必须有一种更有效的方法来做到这一点。

在此方面的任何帮助将不胜感激:)

编辑: 这是我认为是两种类型的头文件的链接:

PoolByteArray / PoolArrays:https://pastebin.com/8ieW0yfS

ByteVector:https://taglib.org/api/tbytevector_8h_source.html

编辑2:

另外,有人指出按值而不是按引用传递大量数据也是相当低效的,这当然是真的。

编辑3:

据我所知,godots 数组没有构造函数可以接受 const char *,所以我不能以这种方式转换它们。

解决方案:

godot::PoolByteArray ByteVector2PoolByte(TagLib::ByteVector && godot::PoolByteArray ByteVector2PoolByte(TagLib::ByteVector && data) {
  godot::PoolByteArray converted_buffer = godot::PoolByteArray();
  const  char* original_buffer = data.data();
  converted_buffer.resize(data.size());
  memcpy((uint8_t*)converted_buffer.write().ptr(), original_buffer, data.size());
  return converted_buffer;
c++ c++17 godot taglib bytebuffer
1个回答
0
投票

使用memcpy向PoolByteArray添加数据

char buf[4500] = {};
PoolByteArray raw;
raw.resize(4500);
memcpy((uint8_t *)raw.write().ptr(), buf, sizeof(buf));
© www.soinside.com 2019 - 2024. All rights reserved.