• notice
  • Congratulations on the launch of the Sought Tech site

Interconversion between QByteArray and char, int, float in QT

1. The source of the problem

To use the SQLite database to save a fixed-length char type array, there may be equal characters in it, so it will definitely lose data when treated as a string varchar, so use binary to save BLOB, so the corresponding QT data type should be processed by QByteArray, It turned out that I only used QByteArray to convert to char* type, and other conversions have not been done yet. I found that there are still some ways to do it. In order to not continue to build wheels in the future, I will write it out and mark it.


2. Conversion between QByteArray and char*


2.1 QByteArray to char*

Method 1 Traditional method data() and size() functions (convenient)

QByteArray array(10, "Q");//Initialization
//array assignment and other code
//...

// convert
char *buf;//just a pointer
int len;//Length of buf
buf = array.data();
len = array.size();

Method 2 memcpy() method (flexible)

QByteArray array(9,"Q");
char buf[10];//Array
int len_array = array.size();
int len_buf = sizeof(buf);
int len = qMin( len_array, len_buf );

// convert
memcpy( buf, array, len );


2.2 char* to QByteArray

Method 1 Use the constructor (convenient)

char buf[10];
// assign value to buf
for (int i = 0; i < 10; i++)
{
    buf[i] = (i + 1) % 3;//There is a "" element in it
}

// convert
QByteArray array;
array = QByteArray(buf, 10);//Because there is `` in buf[], the data length must be written; otherwise, the data will be directly truncated and data will be lost

Method 2 memcpy() method (flexible)

char buf[10];
// assign value to buf
for (int i = 0; i < 10; i++)
{
    buf[i] = (i + 1) % 3;//There is a "" element in it
}

// convert
QByteArray array;
array.resize(sizeof(buf));//Reset data size
memcpy(array.data(), buf, sizeof(buf));//copy data


3. Conversion of QByteArray to int and int[]


3.1. Convert between int and QByteArray

[1] int to QByteArray

// int to QByteArray
int intVar = 199;

QByteArray array;
int len_intVar = sizeof(intVar);
array.resize(len_intVar);
memcpy(array.data(), &intVar, len_intVar);

[2] QByteArray to int

// QByteArray to int
// array data is connected to the above
int outIntVar;
memcpy(&outIntVar, array.data(), len_intVar);
//memcpy(&outIntVar, array, len_intVar);//This line of code is the same as the previous sentence


3.2. Interchange between int[] and QByteArray

[1] int[] to QByteArray

// int[] to QByteArray
// int[] to QByteArray
int intVar[4] = {1,2,9,0};//Initialize variable assignment

QByteArray array;
int len_intVar = sizeof(intVar);
array.resize(len_intVar);
//Convert int[] -> QByteArray
memcpy(array.data(), &intVar, len_intVar);

[2] QByteArray to int[]

// QByteArray to int[]
// array data is connected to the above
int outIntVar[4];
memcpy(&outIntVar, array.data(), len_intVar);
//memcpy(&outIntVar, array, len_intVar);//This line of code is the same as the previous sentence


4. Conversion of QByteArray to float and float[]

In fact, you can refer to Section 3, the usage of int.


4.1. float[] and QByteArray conversion

[1] float[] to QByteArray

// float[] to QByteArray
float fVar[4] = { 1.1, 2.3, 9.5, 0.2 };//Initialize variable assignment

QByteArray array;
int len_fVar = sizeof(fVar); // 4*4 = 16 (a float occupies 4 bytes)
array.resize(len_intVar);
memcpy(array.data(), &fVar, len_fVar);

[2] QByteArray to float[]

// QByteArray to float[]
float outFvar[4];
memcpy(&outIntVar, array.data(), len_fVar);
//memcpy(&outFvar, array, len_fVar);//This line of code is the same as the previous sentence


4.2. Convert between float and QByteArray

It is safe to refer to int.

So far, this article about the mutual conversion between QByteArray and char, int, and float in QT is introduced here. For more information on the mutual conversion between QT QByteArray and char, int, and float, please search for previous articles of Yunhaitian Tutorial or Continue to browse the related articles below and hope that you will support Yunhaitian Tutorial in the future!

Original address: https://blog.csdn.net/humanking7/article/details/80913474



Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+