比如用Long Long存3个数据的内容。

这里要知道大小端的知识点。

方法一是用位运算;

方法二是用指针;

方法三是结构体(本质上也是指针);

运行截图如下:

源码如下:

main.cpp

#include <iostream>
using namespace std;struct SplitLongLong{short shortValue2;short shortValue1;int intValue;
};void main(){long long myTestLongLong=1234605616436508552;cout<<"hexadecimal original data: "<<hex<<"0x:"<<myTestLongLong<<endl;cout<<"decimalism original data:"<<dec<<myTestLongLong<<endl<<endl;//The first method!  cout<<"The first method!"<<endl;int method1_int=(int)(myTestLongLong>>4*8);        //Little-endianshort method1_short1=(short)(myTestLongLong>>2*8);short method1_short2=(short)(myTestLongLong);cout<<"hexadecimal 0x"<<hex<<method1_int<<"   0x"<<method1_short1<<"   0x"<<method1_short2<<endl;cout<<"decimalism "<<dec<<method1_int<<"  "<<method1_short1<<"  "<<method1_short2<<endl<<endl;long long method1_long=((long long)method1_int<<4*8)+((long long)method1_short1<<2*8)+((long long)method1_short2);cout<<"hexadecimal combined data :0x"<<hex<<method1_long<<endl;cout<<"decimalism combined data :"<<dec<<method1_long<<endl<<endl;//The second method!cout<<"The second method!"<<endl;char *ptr=(char *)(&myTestLongLong);int method2_int = *(int *)(ptr+4);     //Little-endianshort method2_short1 = *(short*)(ptr+2);short method2_short2 = *(short*)ptr;cout<<"hexadecimal 0x"<<hex<<method2_int<<"  0x"<<method2_short1<<"  0x"<<method2_short2<<endl;cout<<"decimalism "<<dec<<method2_int<<"  "<<method2_short1<<"  "<<method2_short2<<endl<<endl;long long method2_long;ptr=(char*)(&method2_long);*(short*)ptr=method2_short2;*(short*)(ptr+2)=method2_short1;*(int*)(ptr+4)=method2_int;cout<<"hexadecimal combined data :0x"<<hex<<method2_long<<endl;cout<<"decimalism combined data :"<<dec<<method2_long<<endl<<endl;//The third method!cout<<"The third method!"<<endl;SplitLongLong split;split=*(SplitLongLong*)&myTestLongLong;cout<<"hexadecimal 0x"<<hex<<split.intValue<<"  0x"<<split.shortValue1<<"  0x"<<split.shortValue2<<endl;cout<<"decimalism "<<dec<<split.intValue<<"  "<<split.shortValue1<<"  "<<split.shortValue2<<endl<<endl;long long method3_long;ptr=(char*)(&method3_long);*(short*)ptr=split.shortValue2;*(short*)(ptr+2)=split.shortValue1;*(int*)(ptr+4)=split.intValue;cout<<"hexadecimal combined data :0x"<<hex<<method3_long<<endl;cout<<"decimalism combined data :"<<dec<<method3_long<<endl<<endl;getchar();
}

C++工作笔记-3种方法对数据类型进行拆分(可用于各种协议)相关推荐

  1. C/C++|Qt工作笔记-4种方法判断当前对象(类)名或标识(继承发,typeid法,元对象className()法,Q_CLASSINFO法)

    回想起3个月前,刚刚参加工作也做过类似的笔记,但只有2种方法,估计刚毕业没有什么墨水,经过3个月时间又多了2种方法: 这些方法都可用于RTTI 第一个方法是继承发(C++中很推荐用这个,感觉用这个结构 ...

  2. Qt工作笔记-两种方法从容器中筛选出父类和子类(继承法、typeid法)

    两种方法程序运行界面效果都一样! 程序运行截图如下: 这个图随便看看就可以了,没啥用! 代码如下: widget.h #ifndef WIDGET_H #define WIDGET_H#include ...

  3. 了解员工工作的四种方法

    如何有效的.便捷的了解员工的工作情况是每一个企业管理者都需要面对的管理问题之一.员工,尤其是基层员工,是企业中最大的群体,也承担了公司中最多的工作量,对他们失去关注,将会直接影响公司的长远发展. 杰克 ...

  4. Qt工作笔记-三种方式解决Qt5中文编码问题

    目录 前言 方式一 方式二 方式三 三种方式解决Qt5中文编码问题 前言 这里不谈原理,只说如何去做! 这里本人自己总结下,今天遇到了新的编码问题,在此记录下,方面以后快速调用. 把这三种方法顺序试下 ...

  5. Qt|Linux工作笔记-第二种方式读取Linux中top命令(直接读取,非重定向)

    第一种方式的链接如下: https://mp.csdn.net/postedit/84067805 第一种方式是重定向到文件,然后读取, 第二种方式不重定向到文件,直接读取! 利用QProcess的特 ...

  6. QML工作笔记-2种输入框的使用(TextField与TextInput)

    目录 演示 代码 演示 这里如下图,上个的那个是TextField,下面的那个是TextInput加一个Rectangle 其中上面那个蓝框包着的就是TextField,下面那个是TextInput ...

  7. C++工作笔记-getter/setter方法中大佬的风格

    今天看了Qt官方文档,发现大佬都是这样写代码的, 用更新了我对编码风格的认识, 代码如下: #include <iostream> #include <string> usin ...

  8. 运维测试工作笔记0004---各种免费开源的测试平台

    技术交流QQ群[JAVA,C++,Python,.NET,BigData,AI]:170933152 http://www.luckyframe.cn/book/yhsc/xtgl-28.html 这 ...

  9. Web前端工作笔记006---各种弹框框架

    JAVA技术交流QQ群:170933152 bootbox.js sweetalert2.js

最新文章

  1. php如何实现区分编辑,php实现编辑和保存文件的方法
  2. 开发工具链(国内项目)(持续更)
  3. numberformate php_php number_format函数怎么用?
  4. vue项目安装引入css-loader - cmd篇
  5. Pytorch《DCGAN模型》
  6. [转贴]基于HTTP的QQ协议
  7. 使用 entrySet 遍历 Map 类集合 KV ,而不是 keySet 方式进行遍历的好处
  8. 云-腾讯云-云点播:云点播(VOD)
  9. C++中编译速度与平时代码规范
  10. 立创开源|esp8266三路继电器
  11. 商业变现永不眠(二) — 如何具体规划自己产品的商业化路径?
  12. 鸿鹄云商平台--技术框架
  13. div上下切换(新增、删除、上下div切换)
  14. win10用户注销后该怎么办?如何彻底删除原有的用户名
  15. 【用例设计】接口用例设计
  16. 自然月合同月 生成费用
  17. 基于E-PUCK 2.0多智能体自主协同 高频投影定位系统
  18. xls和 xlsx的区别 xlsx Excel文件怎么转换成 xls文件
  19. matlab计算不同时间步长,Matlab ODE求解器中的时间步长计算
  20. C语言基础语法易错点

热门文章

  1. [实验手册]MPLS/×××分解:防止PE-CE的路由环路
  2. 飞秋教程:日程安排记事提醒
  3. 那时我大约5岁的飞鸽传书
  4. freeeim源码一个个投篮的命中
  5. 微软高级经理:Google Chrome内有部分微软的代码
  6. 融资2.5亿的国产浏览器,被曝只是打包chrome
  7. 大一计算机在线考试,大一计算机考试题(含答案).pdf
  8. 地址已在使用 java_java – UDP地址已经在使用?
  9. jdbc连接云数据库mysql数据库_使用jdbc连接mysql数据库
  10. BATJ原来是这样玩大数据的!