Intertransformation between [QT]QByteArray and char, int, float (and their arrays)

Keywords: SQLite Database Qt Attribute

Original articles are welcome to be reproduced. Reproduced please note: reproduced from uuuuuuuuuuuu Xiangxiang's blog

Links to the original text: https://blog.csdn.net/humanking7/article/details/80913474

1. Sources of the problem

To use the SQLite database to save a fixed length of char array, there may be \0 and other characters in it, so as a string varchar to deal with irreducible lost data, so we need to use binary to save BLOB, so the corresponding type of QT data needs to be processed with QByteArray. Originally, we only used QByteArray to convert to * type. Other transformations have not been done yet, but there are still some doors to find out. In order to stop building wheels in the future, write them out and mark them.

2. Conversion between QByteArray and char*

2.1 QByteArray to char*

Mode 1 data() and size() functions (convenient)

QByteArray array(10, 'Q');//Initialization
//Aray assignment code
//...

// conversion
char *buf;//It's just a pointer.
int len;//Length of buf
buf = array.data();
len = array.size();

Mode 2 memcpy() mode (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 );

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

2.2 char* to QByteArray

Method 1 uses constructors (convenience)

char buf[10];
//Assign a value to buf
for (int i = 0; i < 10; i++)
{
    buf[i] = (i + 1) % 3;//There are'\ 0'elements in it.
}

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

Mode 2 memcpy() mode (flexible)

char buf[10];
//Assign a value to buf
for (int i = 0; i < 10; i++)
{
    buf[i] = (i + 1) % 3;//There are'\ 0'elements in it.
}

// conversion
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. int and QByteArray Interchange

[1] int to QByte Array

// int to QByte Array
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 top
int  outIntVar;
memcpy(&outIntVar, array.data(), len_intVar);
//Memcpy (& outIntVar, array, len_intVar); // This line of code is common to the previous sentence

3.2. int [] and QByteArray interoperability

[1] int[] to QByteArray

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

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

[2]QByteArray to int []

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

4. Conversion of QByteArray to float and float []

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

4.1. Float [] and QByteArray Interchange

[1] float [] to QByte Array

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

QByteArray array;
int len_fVar = sizeof(fVar); // 4*4 = 16 (a float takes 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 common to the previous sentence

4.2. Float and QByteArray Interrotation

You can safely refer to int.

Posted by brax23 on Tue, 01 Jan 2019 13:39:09 -0800