英文:
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 < 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 && 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;
答案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));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论