Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the mutual transformation between QByteArray and char, int and float in QT

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the QT in QByteArray and char, int, float conversion between how to achieve the relevant knowledge, the content is detailed and easy to understand, simple and fast operation, with a certain reference value, I believe that after reading this QT QByteArray and char, int, float conversion between the article will have a harvest, let's take a look at it.

1. Source of problem

To use the SQLite database to save a char array of fixed length, which may contain characters such as\ 0, so you are sure to lose data as a string varchar, so you need to save BLOB in binary, so the corresponding QT data type needs to be processed with QByteArray. Originally, only QByteArray is converted to char*.

Conversion from 2.QByteArray to char* 2.1 QByteArray to char*

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

QByteArray array (10,'Q'); / initialize / / array assignment code / / convert char * buf;// is just the length of a pointer int len;//buf buf = array.data (); len = array.size ()

Mode 2 memcpy () mode (flexible)

QByteArray array; 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 using constructor (convenient)

Char buf [10]; / / assign for to buf (int I = 0; I

< 10; i++){ buf[i] = (i + 1) % 3;//其中存在'\0'元素}// 转化QByteArray array;array = QByteArray(buf, 10);//因为buf[]中有`\0`,必须要写上数据长度;否则,数据会直接截断,丢失数据 方式2 memcpy()方式 (灵活) char buf[10];//给buf赋值for (int i = 0; i < 10; i++){ buf[i] = (i + 1) % 3;//其中存在'\0'元素}// 转化QByteArray array;array.resize(sizeof(buf));//重置数据大小memcpy(array.data(), buf, sizeof(buf));//copy数据3.QByteArray与int 以及int[] 的转换3.1. int 与 QByteArray 互转 [1] int 转 QByteArray // int 转 QByteArrayint intVar = 199;QByteArray array;int len_intVar = sizeof(intVar);array.resize(len_intVar);memcpy(array.data(), &intVar, len_intVar); [2]QByteArray 转 int // QByteArray 转 int// array 数据接上面int outIntVar;memcpy(&outIntVar, array.data(), len_intVar);//memcpy(&outIntVar, array, len_intVar);//此行代码与上句通用3.2. int[] 与 QByteArray 互转 [1] int[] 转 QByteArray // int[] 转 QByteArray// int[] 转 QByteArrayint intVar[4] = {1,2,9,0};//初始化变量赋值QByteArray array;int len_intVar = sizeof(intVar);array.resize(len_intVar);//转换 int[] ->

QByteArraymemcpy (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); / / the conversion of this line of code with the previous sentence of general 4.QByteArray, float and float []

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

4.1. Interchange between float [] and QByteArray

[1] float [] to QByteArray

/ / float [] to QByteArrayfloat fVar [4] = {1.1,2.3,9.5,0.2}; / / initialization variable assignment QByteArray array;int len_fVar = sizeof (fVar); / / 4room4 = 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 4.2 common to the previous sentence. Float and QByteArray transfer each other

You can safely refer to int.

This is the end of the article on "how to realize the transformation between QByteArray and char, int and float in QT". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to realize the transformation between QByteArray and char, int and float in QT". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report