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

huangapple go评论75阅读模式
英文:

Is there a better way to convert between two Byte Buffers of two different classes?

问题

下面是代码部分的中文翻译:

// 将TagLib的ByteVector转换为Godot的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;
}

这是你当前用来将TagLib的ByteVector转换为Godot的PoolByteArray的方式。如果你想寻找更高效的方法,你可以考虑以下代码:

// 更高效的将TagLib的ByteVector转换为Godot的PoolByteArray
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;
}

这个修改后的代码使用memcpy函数来直接复制数据,从而更加高效地进行转换。

英文:

So, I'm using the TagLib Library in combination with the Godot Engine and I want to send big amounts of Data between those two in the form of Byte Arrays. The Problem is both have their own variables to store Bytes:

Godot: PoolByteArray
TagLib: ByteVector

To return Data from TagLib functions into my Godot Project I have to convert from ByteVector to PoolByteArray.

My current way of doing it is like this:

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

This seems terrible inefficient to me and I thought there has to be a more efficient way of doing this.

Any help on this would be really appreciated 有没有更好的方法来在两个不同类的字节缓冲之间进行转换?

EDIT:
Heres a link to waht I think is the header file for both types:

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

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

EDIT 2:

Also, someone pointed out that passing that huge amount of data by value instead of by reference is also pretty ineffecient, which is of course true.

EDIT 3:

As far as I can tell there is no constructor for godots array that would take const char * so I can't convert them this way.

SOLUTION:

godot::PoolByteArray ByteVector2PoolByte(TagLib::ByteVector &amp;&amp; godot::PoolByteArray ByteVector2PoolByte(TagLib::ByteVector &amp;&amp; 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;

答案1

得分: 0

使用memcpy将数据添加到PoolByteArray

char buf[4500] = {};
PoolByteArray raw;
raw.resize(4500);
memcpy((uint8_t *)raw.write().ptr(), buf, sizeof(buf));
英文:

Use memcpy to add data to PoolByteArray:

char buf[4500] = {};
PoolByteArray raw;
raw.resize(4500);
memcpy((uint8_t *)raw.write().ptr(), buf, sizeof(buf));

huangapple
  • 本文由 发表于 2023年2月27日 06:53:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定