简体   繁体   中英

QByteArray to unsigned char does not fully copy

I need to convert the QByteArray of a QImage into unsigned char *.

I do it by:

QByteArray data;
QBuffer buffer(&data);

buffer.open(QIODevice::WriteOnly);
convImage.save(&buffer, "PNG");
buffer.close()

unsigned char *data_image = (unsigned char *)malloc(data.size());
memcpy(data_image, reinterpret_cast<unsigned char *>(data.data()), (unsigned int)data.size());

But the result of data_image compared to the QByteArray of the image is like this:

在此处输入图片说明

data = (QByteArray of the Image)

data_image = unsigned char* from data

I think it's not fully copied. I need the unsigned char* data for an external DLL function.

You do not see the full length of the data_image array, because it is just a pointer and there is no way to know what its actual size is. So the debugger is applying some standard rules to stop when it thinks it should. On such rule is to stop on a NULL byte.

If you are using Qt Creator, there are several ways you can force the debugger to show more data:

  1. Right-click on the variable you are interested in and select Change Value Display Format > Array of xxx items . This is the easiest way, but it is limited as you can only select 10, 100, 1,000 or 10,000 items.
  2. Right-click on the variable you are interested in and select Open Memory Editor > Open Memory View At Object Address . This is the most versatile as you can see everything in memory, but it might be more confusing if you are not used to play with memory views.

If you have QImage, you can do it simpler:

unsigned char *data_image = (unsigned char *)malloc(convImage.sizeInBytes());
memcpy(data_image, convImage.bits(), convImage.sizeInBytes());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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