基础笔记1 —— float型数据与其他类型数据的转换问题
不多说,直接上代码。
发布日期:2021-07-01 04:04:04
浏览次数:3
分类:技术文章
本文共 2480 字,大约阅读时间需要 8 分钟。
文章目录
1.查看 float 型数据在内存中的二进制存储形式
#include#include #define uchar unsigned charusing namespace std;void binary_print(uchar c){ for (int i = 0; i < 8; ++i) { if ((c << i) & 0x80) cout << '1'; else cout << '0'; } cout << ' ';}void main(){ float a; uchar c_save[4]; uchar i; void *f; f = &a; cout << "请输入一个浮点数:"; cin >> a; cout << endl; for (i = 0; i<4; i++) { c_save[i] = *((uchar*)f + i); } cout << "此浮点数在计算机内存中储存格式如下:" << endl; for (i = 4; i != 0; i--) binary_print(c_save[i - 1]); cout << endl;}
2.float类型数据与十六进制的转换
#includeint main(){ // 将十六进制转换为float形式 unsigned char pMem[] = { 0x66,0xE6,0xF0,0x42 }; float *p = (float*)pMem; printf("%g\r\n", *p); // 将float转换为16进制 float a = 120.45f; unsigned char * b = (unsigned char*)&a; for (int i = 0; i < 4; i++) printf("0x%2X,", b[i]); system("pause"); return 0;}
3.float型数据拆分成2个short型数据并还原
// Author:Jack Soong#include#include using namespace std;int main(){ float a = 17.625; short *b = (short *)&a; //此处需要注意,由于CPU架构不同,可能会采用不同的存储方式,也就是常说的大小端存储问题 cout << " 高16: " << hex << b[1] << " 低16: " << hex < <
4.在C++中byte与int的互相转换
#include/** * htonl 表示 host to network long ,用于将主机 unsigned int 型数据转换成网络字节顺序; * htons 表示 host to network short ,用于将主机 unsigned short 型数据转换成网络字节顺序; * ntohl、ntohs 的功能分别与 htonl、htons 相反。 */ /** * byte 不是一种新类型,在 C++ 中 byte 被定义的是 unsigned char 类型; * 但在 C# 里面 byte 被定义的是 unsigned int 类型 */typedef unsigned char byte;/** * int 转 byte * 方法无返回的优点:做内存管理清爽整洁 * 如果返回值为 int,float,long,double 等简单类型的话,直接返回即可 * 总的来说,这真心是一种很优秀的方法设计模式 */void int2bytes(int i, byte* bytes, int size = 4);// byte 转 int int bytes2int(byte* bytes, int size = 4);void int2bytes(int i, byte* bytes, int size) { // byte[] bytes = new byte[4]; memset(bytes, 0, sizeof(byte) * size); bytes[0] = (byte)(0xff & i); bytes[1] = (byte)((0xff00 & i) >> 8); bytes[2] = (byte)((0xff0000 & i) >> 16); bytes[3] = (byte)((0xff000000 & i) >> 24);}int bytes2int(byte* bytes, int size) { int iRetVal = bytes[0] & 0xFF; iRetVal |= ((bytes[1] << 8) & 0xFF00); iRetVal |= ((bytes[2] << 16) & 0xFF0000); iRetVal |= ((bytes[3] << 24) & 0xFF000000); return iRetVal;}
https://www.cnblogs.com/shangbolei/p/4441946.html
https://blog.csdn.net/renanrenan/article/details/83094956
https://www.cnblogs.com/java20130723/p/3212023.html
转载地址:https://miracle.blog.csdn.net/article/details/92778114 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年05月01日 10时26分08秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
字符串的排序
2019-05-01
内存分配(mallloc,calloc,realloc,new)
2019-05-01
使用 Minidumps 和 Visual Studio .NET 进行崩溃后调试
2019-05-01
struts返回xml数据例子
2019-05-01
内存对齐详解
2019-05-01
秋招总结(一)-C++归纳
2019-05-01
秋招总结(三)-操作系统归纳
2019-05-01
带缓冲I/O 和不带缓冲I/O的区别与联系
2019-05-01
LINUX CP命令详解
2019-05-01
source insight快捷键及使用技巧
2019-05-01
映 射 ALT 键
2019-05-01
vim使用快捷键F4生成文件头注释、F5生成main函数模板、F6生成.h文件框架模板
2019-05-01
OV5620的视频驱动
2019-05-01
C++中两个类交叉定义或递归定义的解决办法
2019-05-01
ECharts is not Loaded解决方案
2019-05-01
echarts切换tab时,第一个图表显示,第二个图表不显示的解决办法
2019-05-01
记一次Hive 行转列 引起的GC overhead limit exceeded
2019-05-01
OpenGL ES八 - 交叉存取顶点数据
2019-05-01
crontab定时任务写法
2019-05-01