简体   繁体   English

将 QByteArray 转换为 uint8_t*(uint8_t array)

[英]Convert QByteArray to uint8_t*(uint8_t array)

How can I convert QByteArray coming from QFile::readAll() into a uint8_t array( uint8_t* )?如何将来自QFile::readAll()QByteArray转换为uint8_t数组( uint8_t* )?

I tried static_cast<>() but didn't seemed to work for whole array.我尝试了static_cast<>()但似乎不适用于整个数组。

examples of what I've done with static_cast :我用static_cast做了什么的例子:

uint8_t* x = new uint8_t;
x[0] = static_cast<uint8_t>(testWav.readAll()[0]);
uint8_t* x = new uint8_t;
*x = static_cast<uint8_t>(*testWav.readAll());

etc. didn't work等没用

This doesn't do what you want.这不会做你想要的。

 uint8_t* x = new uint8_t;
 x[0] = static_cast<uint8_t>(testWav.readAll()[0]);

This way you copy only [0] element of array.这样你只复制数组的 [0] 元素。 The rest of allocated memory stay uninitialized分配给 memory 的 rest 保持未初始化状态

This cannot work at all:这根本行不通:

uint8_t* x = new uint8_t;
*x = static_cast<uint8_t>(*testWav.readAll());

QByteArray is a class-type of dynamic container, so you can't use static_cast to access its data as an array, it's a principally different, incompatible data structure. QByteArray是一种类类型的动态容器,因此您不能使用static_cast将其数据作为数组访问,它是一种主要不同的、不兼容的数据结构。 And you're still assigning only the first element: *x is an equivalent of x[0] .而且您仍然只分配第一个元素: *x相当于x[0]

QByteArray has method QByteArray::data() which returns a pointer to char* : https://doc.qt.io/qt-6/qbytearray.html#data QByteArray有方法QByteArray::data()返回一个指向char*的指针: https://doc.qt.io/qt-6/qbytearray.html#data

The pointer returned by data() isn't owning one and points to memory allocated by class, so data contained in QByteArray which returned by readAll() will cease to exist at end of statement containing it, unless it's assigned to a more persistent storage: data()返回的指针不拥有一个指针,而是指向由 class 分配的 memory,因此readAll()返回的QByteArray中包含的数据将在包含它的语句结束时不复存在,除非它被分配给更持久的存储:

QByteArray fileContent = file.readAll();
char *buffer = fileContent.data();

Result of fileContent.size() would give the actual size of buffer. fileContent.size()的结果将给出缓冲区的实际大小。 Note that in some cases it's more convenient to work with QByteArray instead of a raw pointer.请注意,在某些情况下,使用 QByteArray 而不是原始指针更方便。 You need conversion ONLY if you have to use some API taking raw pointer or face some performance issues.仅当您必须使用一些 API 获取原始指针或面临一些性能问题时才需要转换。

I think first you need to know that a pointer is not an array.我想首先你需要知道指针不是数组。 And that you cannot assing a c-array as a whole.而且你不能将 c 数组作为一个整体进行评估。

Then you should reconsider if you actually need to convert anything.然后你应该重新考虑你是否真的需要转换任何东西。 QByteArray grants you access to the underlying array via its data member. QByteArray授予您通过其data成员访问底层数组的权限。 (I find it a bit unfortunate that QByteArray is based on char rather than unsigned char , so you'd want to check whether char is signed or not). (我发现QByteArray基于char而不是unsigned char有点不幸,所以你想检查char是否有符号)。

Eventually, if you still want, you can write a loop to copy elements from the QByteArray to another array.最后,如果您仍然需要,可以编写一个循环将元素从QByteArray复制到另一个数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM