Qt5.12实战之QByteArray与字符指针及字符串转换

示例源码:

#include <QCoreApplication>
#include <QDebug>
#include <QTextStream>
static QTextStream cout (stdout,QIODevice::WriteOnly);
#include <iostream>
#include <QtGlobal>
#include <QByteArray>

void test()
{
    qDebug() << "HelloWorld-qdebug";
    cout << "Helloword-QTextStream" <<endl;
    std::cout<<"Helloword-std::cout"<<std::endl;
}

void test_types()
{
    qint8 signed_char_type;//signed char 有符号8bit
    qint16 signed_short_type;//signed short 有符号16bit
    qint32 signed_int_type;//signed int 有符号32bit
    qint64 signed_long_long_int_type;//signed long long int 有符号64bit

    qDebug() <<"sizeof(qint8)="  <<sizeof (signed_char_type)
             <<"sizeof(qint16)=" <<sizeof (signed_short_type)
             <<"sizeof(qint32)=" <<sizeof(signed_int_type)
             <<"sizeof(qint64)=" <<sizeof (signed_long_long_int_type)<<endl;;

    qptrdiff qint32_or_qint64_type;//32位系统 qint32 64位系统 qint64
    qreal double_or_float_type;//默认是double,如果加上-qreal float选项,那么是float
    qintptr same_as_qptrdiff;//32位系统 qint32 64位系统 qint64
    quintptr quint32_or_quint64_type;//32位系统 quint32 64位系统 quint64
    qDebug() <<"sizeof(qptrdiff)="  <<sizeof (qint32_or_qint64_type)
             <<"sizeof(qreal)=" <<sizeof (double_or_float_type)
             <<"sizeof(qintptr)=" <<sizeof(same_as_qptrdiff)
             <<"sizeof(quintptr)=" <<sizeof (quint32_or_quint64_type)<<endl;

    quint8 unsigned_char_type;//unsigned char 无符号8bit
    quint16 unsigned_short_type;//unsigned short 无符号16bit
    quint32 unsigned_int_type;//unsigned int 无符号32bit
    quint64 unsigned_long_long_int_type;//unsigned long long int 无符号64bit
    qDebug() <<"sizeof(quint8)="  <<sizeof (unsigned_char_type)
             <<"sizeof(quint16)=" <<sizeof (unsigned_short_type)
             <<"sizeof(quint32)=" <<sizeof(unsigned_int_type)
             <<"sizeof(quint64)=" <<sizeof (unsigned_long_long_int_type)<<endl;

    qlonglong same_as_qint64_type;//相当于qint64
    qulonglong same_as_quint64_type;//相当于quint64
    qDebug() <<"sizeof(qlonglong)="  <<sizeof (same_as_qint64_type)
             <<"sizeof(qulonglong)=" <<sizeof (same_as_quint64_type)<<endl;

    uchar unsigned_char_type_same_quint8;//unsigned char 无符号8bit
    ushort unsigned_short_type_same_quint16;//unsigned short 无符号16bit
    uint unsigned_int_type_same_quint32;//unsigned int 无符号32bit
    ulong unsigned_long_type;//unsigned long 无符号32bit
    qDebug() <<"sizeof(uchar)="  <<sizeof (unsigned_char_type_same_quint8)
             <<"sizeof(ushort)=" <<sizeof (unsigned_short_type_same_quint16)
             <<"sizeof(uint)=" <<sizeof(unsigned_int_type_same_quint32)
             <<"sizeof(ulong)=" <<sizeof (unsigned_long_type)<<endl;

}



void test_qbytearray()
{
    QByteArray bArray("Hello QByteArray"); // '\0'
    int len = bArray.length();
    char cLast = bArray[17];
    qDebug()<<"string len:"
            <<len
            <<"last char:"
            <<cLast
           <<endl;
}

void test_qbytearray_resize()
{
    QByteArray ba;
    ba.resize(3);
    ba[0]=0x3c;
    ba[1]=0xb8;
    ba[2]=0x64;
    qDebug()<<endl<<"ba length:"<<ba.length()<<endl;
}

void test_qbytearray_fill()
{
    QByteArray ba("hello");
    qDebug()<<"ba before fill:"<<ba<<endl;
    ba.fill('K');
    qDebug()<<"ba after fill:"<<ba<<endl;
    ba.fill('K',2);
    qDebug()<<"ba after fill two char:"<<ba<<endl;
    ba.fill('Q',10);
    qDebug()<<"ba after fill ten char Q:"<<ba
           <<"qbytearray length:"<<ba.length()
           <<"sizeof(ba)="<<sizeof (ba)<<endl //ba is a point of QByteArray
           <<"sizeof(QByteArray[])="<<sizeof ( (char)ba[1])<<endl;
    ba.data()[7]='A';
    qDebug()<<ba.at(4)<<ba.data()[5]<<ba.constData()[7]<<endl;
}

void test_qbytearray_right_left_mid()
{
    QByteArray ba("HelloWorld2023");
    qDebug()<<ba.left(5);
    qDebug()<<ba.right(4);
    qDebug()<<ba.mid(5,5);
}

void test_qbytearray_size()
{
    QByteArray ba("HelloWorld");
    qDebug()<<ba.length();
    qDebug()<<ba.size();
    qDebug()<<ba.count();
    QByteArray ba1("He\0llo");
    qDebug()<<ba1.length();
}

void test_datatype_convert()
{
    QByteArray ba("HelloWorld");
    QByteArray ba_hex =  ba.toHex();
    qDebug() << "HelloWorld to Hex: "<<ba_hex<<endl; //48656c6c6f576f726c64

    QByteArray ba_fromhex = QByteArray::fromHex("48656c6c6f576f726c64");
    qDebug() << "QByteArray from Hex: "<<ba_fromhex<<endl;

    int digit = 255;
    uint u_digit = 255u;
    qlonglong qll_digit = 255ll;
    qulonglong qull_digit = 255ull;
    qDebug()<<"number base 10 on int :"<< QByteArray::number(digit);
    qDebug()<<"number base 10 on uint :"<<QByteArray::number(u_digit);
    qDebug()<<"number base 10 on qlonglong :"<<QByteArray::number(qll_digit);
    qDebug()<<"number base 10 on qulonglong :"<<QByteArray::number(qull_digit);

    qDebug()<<"number base 10 on int :"<< QByteArray::number(78);
    qDebug()<<"number base 10 on uint :"<<QByteArray::number(78u);
    qDebug()<<"number base 10 on qlonglong :"<<QByteArray::number(78ll);
    qDebug()<<"number base 10 on qulonglong :"<<QByteArray::number(78ull);

    qDebug()<<"number base 10 on int :"<< QByteArray::number(digit,16);
    qDebug()<<"number base 10 on uint :"<<QByteArray::number(u_digit,16);
    qDebug()<<"number base 10 on qlonglong :"<<QByteArray::number(qll_digit,16);
    qDebug()<<"number base 10 on qulonglong :"<<QByteArray::number(qull_digit,16);

    qDebug()<<"number base 10 on int :"<< QByteArray::number(digit,8);
    qDebug()<<"number base 10 on uint :"<<QByteArray::number(u_digit,8);
    qDebug()<<"number base 10 on qlonglong :"<<QByteArray::number(qll_digit,8);
    qDebug()<<"number base 10 on qulonglong :"<<QByteArray::number(qull_digit,8);

    qDebug()<<"number base 10 on int :"<< QByteArray::number(digit,2);
    qDebug()<<"number base 10 on uint :"<<QByteArray::number(u_digit,2);
    qDebug()<<"number base 10 on qlonglong :"<<QByteArray::number(qll_digit,2);
    qDebug()<<"number base 10 on qulonglong :"<<QByteArray::number(qull_digit,2);

    QByteArray num;
    qDebug()<<"QByteArray::setNum base 10 on int :"<< num.setNum(digit);
    qDebug()<<"QByteArray::setNum base 10 on uint :"<<num.setNum(u_digit);
    qDebug()<<"QByteArray::setNum base 10 on qlonglong :"<<num.setNum(qll_digit);
    qDebug()<<"QByteArray::setNum base 10 on qulonglong :"<<num.setNum(qull_digit);

    qDebug()<<"QByteArray::setNum base 10 on int :"<< num.setNum(digit,16);
    qDebug()<<"QByteArray::setNum base 10 on uint :"<<num.setNum(u_digit,16);
    qDebug()<<"QByteArray::setNum base 10 on qlonglong :"<<num.setNum(qll_digit,16);
    qDebug()<<"QByteArray::setNum base 10 on qulonglong :"<<num.setNum(qull_digit,16);

    qDebug()<<"QByteArray::setNum base 10 on int :"<< num.setNum(digit,8);
    qDebug()<<"QByteArray::setNum base 10 on uint :"<<num.setNum(u_digit,8);
    qDebug()<<"QByteArray::setNum base 10 on qlonglong :"<<num.setNum(qll_digit,8);
    qDebug()<<"QByteArray::setNum base 10 on qulonglong :"<<num.setNum(qull_digit,8);

    qDebug()<<"QByteArray::setNum base 10 on int :"<< num.setNum(digit,2);
    qDebug()<<"QByteArray::setNum base 10 on uint :"<<num.setNum(u_digit,2);
    qDebug()<<"QByteArray::setNum base 10 on qlonglong :"<<num.setNum(qll_digit,2);
    qDebug()<<"QByteArray::setNum base 10 on qulonglong :"<<num.setNum(qull_digit,2);

    double dbNum=12345.66;
    qDebug()<<"number base 10 on double of e format(prec=6) :"<< QByteArray::number(dbNum,'e');
    qDebug()<<"number base 10 on double of e format(prec=3) :"<< QByteArray::number(dbNum,'e',3);
    qDebug()<<"number base 10 on double of E format(prec=6) :"<< QByteArray::number(dbNum,'E');
    qDebug()<<"number base 10 on double of e format(prec=3) :"<< QByteArray::number(dbNum,'E',3);
    qDebug()<<"number base 10 on double of f format(prec=6) :"<< QByteArray::number(dbNum,'f');
    qDebug()<<"number base 10 on double of f format(prec=3) :"<< QByteArray::number(dbNum,'f',3);
    qDebug()<<"number base 10 on double of f format(prec=3) :"<< QByteArray::number(dbNum,'f',1);
    qDebug()<<"number base 10 on double of g format(prec=6) :"<< QByteArray::number(dbNum,'g');
    qDebug()<<"number base 10 on double of g format(prec=3) :"<< QByteArray::number(dbNum,'g',3);
    qDebug()<<"number base 10 on double of G format(prec=6) :"<< QByteArray::number(dbNum,'G');
    qDebug()<<"number base 10 on double of G format(prec=3) :"<< QByteArray::number(dbNum,'G',3);

    QByteArray abc="HelloWorld";
    qDebug()<<"HelloWorld ->toLower() : "<<abc.toLower();
    qDebug()<<"HelloWorld ->toUpper() : "<<abc.toUpper();

    QByteArray strNum="-123456";
    QByteArray strNumU="123456";
    qDebug()<<"strNum ->toInt() : "<<strNum.toInt();
    qDebug()<<"strNum ->toUInt() : "<<strNumU.toUInt();


    bool isBase=false;
    QByteArray strDigit="255";
    QByteArray strBin="11111111";
    qDebug()<<"digit ->toInt() base on 10 : "<<strDigit.toInt(&isBase)<<",isBase:"<<isBase;
    qDebug()<<"digit ->toInt() base on 16 : "<<strDigit.toInt(&isBase,16)<<",isBase:"<<isBase;
    qDebug()<<"digit ->toInt() base on 8 : "<<strDigit.toInt(&isBase,8)<<",isBase:"<<isBase;
    qDebug()<<"digit ->toInt() base on 2 : "<<strBin.toInt(&isBase,2)<<",isBase:"<<isBase;

    QByteArray strDouble="3.14159";
    qDebug()<<"strDouble ->toDouble() : "<<strDouble.toDouble(&isBase)<<",isBase:"<<isBase;
    qDebug()<<"strDouble ->toFloat() : "<<strDouble.toFloat(&isBase)<<",isBase:"<<isBase;
    qDebug()<<QByteArray::number(strDouble.toDouble());
    qDebug()<<QByteArray::number(strDouble.toDouble(),'g');
    qDebug()<<QByteArray::number(strDouble.toDouble(),'g',3);
    qDebug()<<QByteArray::number(strDouble.toDouble(),'G');
    qDebug()<<QByteArray::number(strDouble.toDouble(),'G',3);
    qDebug()<<QByteArray::number(strDouble.toDouble(),'f');
    qDebug()<<QByteArray::number(strDouble.toDouble(),'f',3);
    qDebug()<<QByteArray::number(strDouble.toDouble(),'e');
    qDebug()<<QByteArray::number(strDouble.toDouble(),'e',3);
    qDebug()<<QByteArray::number(strDouble.toDouble(),'E');
    qDebug()<<QByteArray::number(strDouble.toDouble(),'E',3);
}

typedef struct User{
    int id;
    std::string name;
}USER;

void test_qbytearray_charpoint_std_string_qstring()
{
    USER u;
    u.id=1001;
    u.name="hello";

    QByteArray arr;
    arr.append((char*)&u,sizeof (u));

    USER *pUser = (USER*)arr.data();
    qDebug() <<pUser->id<<pUser->name.c_str();

    QByteArray ba("HelloWorld");
    char *pBa = ba.data();
    qDebug() << pBa;
    qDebug() << *pBa;
    while (*pBa) {
        qDebug()<< "item of Array : " << *pBa;
        ++pBa;
    }
    char *p = &ba.data()[5];
    qDebug() <<p;

    std::string str = ba.toStdString();
    qDebug()<<"std::string ->"<<str.c_str();

    QByteArray baStd = QByteArray::fromStdString(std::string("this is a std string"));
    qDebug()<<"fromStdString ->"<<baStd;

    QString qstr = QString("HelloWorld");
    qDebug()<<"QString to QByteArray ->"<<qstr.toLatin1();
    QByteArray tmpBa = "flkdsjlkfjkdsjfldsjkfjk";
    qstr=tmpBa;
    qDebug()<<qstr;

    if(tmpBa.isEmpty())
    {
        qDebug()<<"";
    }
    if(QByteArray("").isEmpty())
    {
        qDebug()<<"";
    }
    if(QByteArray().isEmpty())
    {
        qDebug()<<"";
    }


}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    test();
    test_types();
    test_qbytearray();
    test_qbytearray_resize();
    test_qbytearray_fill();
    test_qbytearray_right_left_mid();
    test_qbytearray_size();

    test_datatype_convert();
    test_qbytearray_charpoint_std_string_qstring();
    return a.exec();
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/923.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

进程调度的基本过程

这里写目录标题什么是进程进程管理结构体或类的主要属性pid内存指针文件描述符表辅助进程调度的属性并发并行并发什么是进程 进程是操作系统对一个正在运行的程序的一种抽象&#xff0c;也就是说&#xff0c;一个运行起来的程序就是一个进程。 进程又是操作系统进行资源分配的…

百度终于要出手了?文心一言

文心一言 百度全新一代知识增强大语言模型&#xff0c;文心大模型家族的新成员&#xff0c;能够与人对话互动&#xff0c;回答问题&#xff0c;协助创作&#xff0c;高效便捷地帮助人们获取信息、知识和灵感。 前几天炒的风风火火的ChatGPT&#xff0c;虽然 ChatGPT 很强大&a…

【Error: ImagePullBackOff】Kubernetes中Nginx服务启动失败排查流程

❌pod节点启动失败&#xff0c;nginx服务无法正常访问&#xff0c;服务状态显示为ImagePullBackOff。 [rootm1 ~]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx-f89759699-cgjgp 0/1 ImagePullBackOff 0 103…

【数据结构与算法】顺序表和链表

[数据结构与算法]顺序表和链表线性表线性表定义&#xff1a;顺序表静态顺序表动态顺序表动态顺序表的接口实现链表链表的概念链表的分类单向链表的接口实现双向链表循环的接口实现顺序表和链表的区别缓存利用率参考存储体系结构以及局部原理性存储体系结构Cache采用的程序访问的…

面试官问 : ArrayList 不是线程安全的,为什么 ?(看完这篇,以后反问面试官)

前言 金三银四 &#xff1f; 也许&#xff0c;但是。 近日&#xff0c;又收到金三银四一线作战小队成员反馈的战况 &#xff1a; 我不管你从哪里看的面经&#xff0c;但是我不允许你看到我这篇文章之后&#xff0c;还不清楚这个面试问题。 本篇内容预告&#xff1a; Array…

【基础算法】单链表的OJ练习(5) # 环形链表 # 环形链表II # 对环形链表II的解法给出证明(面试常问到)

文章目录前言环形链表环形链表 II写在最后前言 本章的OJ练习相对于OJ练习(4)较为简单。不过&#xff0c;本章的OJ最重要的是要我们证明为何可以这么做。这也是面试中常出现的。 对于OJ练习(4)&#xff1a;-> 传送门 <-&#xff0c;分割链表以一种类似于归并的思想解得&a…

ChatGPT-4 终于来了(文末附免费体验地址)

大家好&#xff0c;我是小钱学长。 ChatGPT4.0 重磅来袭&#xff0c;今天一打开plus页面出现的就是这个GPT-4的体验界面&#xff01;现在就带大家一起看看GPT4.0​。 进入之后是这样的 看到最下面有一行话&#xff0c;目前应该是4个小时限制100条消息。 GPT-4有什么优势&…

手把手学会DFS (递归入门)

目录 算法介绍 递归实现指数型枚举 递归实现排列型枚举 递归实现组合型枚举 算法介绍 &#x1f9e9;DFS 即 Depth First Search &#xff0c;中文又叫深度优先搜索&#xff0c;是一种沿着树的深度对其进行遍历&#xff0c;直到尽头之后再进行回溯&#xff0c;再走其他路线的…

springboot复习(黑马)

学习目标基于SpringBoot框架的程序开发步骤熟练使用SpringBoot配置信息修改服务器配置基于SpringBoot的完成SSM整合项目开发一、SpringBoot简介1. 入门案例问题导入SpringMVC的HelloWord程序大家还记得吗&#xff1f;SpringBoot是由Pivotal团队提供的全新框架&#xff0c;其设计…

GPT-4技术报告

摘要 链接&#xff1a;https://cdn.openai.com/papers/gpt-4.pdf 我们汇报了GPT-4的发展&#xff0c;这是一个大规模的多模态模型&#xff0c;可以接受图像和文本输入并产生文本输出。虽然在许多现实场景中&#xff0c;GPT-4的能力不如人类&#xff0c;但它在各种专业和学术基…

数智链接,新一代校园招聘解决方案

疫情3年市场巨变&#xff0c;00后新生代初登上求职舞台&#xff0c;中和作用下&#xff0c;牛客发现新生代求职发生明显变化&#xff0c;企业校招也要随之而变&#xff0c;并率先提出以种草、精准、专业为特点的新一代校园招聘解决方案。01.学生求职变了&#xff01;安全感、非…

奇异值分解(SVD)原理与在降维中的应用

奇异值分解(SVD)原理与在降维中的应用 奇异值分解(Singular Value Decomposition&#xff0c;以下简称SVD)是在机器学习领域广泛应用的算法&#xff0c;它不光可以用于降维算法中的特征分解&#xff0c;还可以用于推荐系统&#xff0c;以及自然语言处理等领域。是很多机器学习算…

GPT-4来袭:开启人工智能新时代

文章目录介绍GPT4 模型演示示例示例 1示例 2示例 3示例 4示例 5最后Reference介绍 2023年3月15日&#xff0c;OpenAI公司正式发布了先进的自然语言处理模型GPT-4&#xff0c;前不久发布的GPT-3.5模型只能理解文字的语言模型&#xff0c;而新发布的GPT4则是多模态模型&#xff…

【java】了解常见集合类

了解常见集合类 一、集合类框架 1、集合类框架结构图 首先我们要对集合类结构有一个大体的认识&#xff0c;所有集合都继承于迭代器&#xff0c;分为单列集合和映射集合&#xff0c;单列集合分为有序可重复和有序不可重复&#xff0c;大概结构如下图所示 2、主要集合类的介…

你真的知道如何系统高效地学习数据结构与算法吗?

文章目录前言&#xff1a;什么是数据结构&#xff1f;什么是算法&#xff1f;学习这个算法需要什么基础&#xff1f;学习的重点在什么地方&#xff1f;一些可以让你事半功倍的学习技巧1.边学边练&#xff0c;适度刷题2.多问、多思考、多互动3.打怪升级学习法4.知识需要沉淀&…

文心一言---中国版的“ChatGPT”狂飙的机会或许要出现了

⭐️我叫忆_恒心&#xff0c;一名喜欢书写博客的在读研究生&#x1f468;‍&#x1f393;。 如果觉得本文能帮到您&#xff0c;麻烦点个赞&#x1f44d;呗&#xff01; 近期会不断在专栏里进行更新讲解博客~~~ 有什么问题的小伙伴 欢迎留言提问欧&#xff0c;喜欢的小伙伴给个三…

linux 基础

1.Shell 命令的格式如下&#xff1a;command -options [argument]command: Shell 命令名称。options&#xff1a; 选项&#xff0c;同一种命令可能有不同的选项&#xff0c;不同的选项其实现的功能不同。argument&#xff1a; Shell 命令是可以带参数的&#xff0c;也可以不带参…

【计算机二级Python】综合题目

计算机二级python真题 文章目录计算机二级python真题一、简单应用——明星投票二、综合应用题《评奖学金 两问》一、简单应用——明星投票 描述使用字典和列表型变量完成最有人气的明星的投票数据分析。投票信息由附件里的文件vote.txt给出,一行只有一个明星姓名的投票才是有效…

【BLE 5.3无线MCU CH582】1、初识CH582开发板(开箱)

1、认识板子 优点&#xff1a; &#xff08;1&#xff09;引脚全部引出&#xff1b; &#xff08;2&#xff09;USB下载程序&#xff1b; &#xff08;3&#xff09;TYPE-C接口好评&#xff1b; &#xff08;4&#xff09;板载连个两个USB口&#xff0c;都可以供电&#xff1b;…

前端性能优化之HTTP缓存

前端缓存 前端缓存可分为两大类&#xff1a;HTTP 缓存和浏览器缓存。 我们今天重点是 HTTP 缓存&#xff0c;下面这张图是前端缓存的一个大致知识点&#xff1a; HTTP 缓存 首先解决困扰绕人们的老大难问题&#xff1a; 一、什么是HTTP缓存&#xff1f; HTTP 缓存会存储与请…
最新文章