常用的数据类型转换

xiaoxiao2025-06-04  46

最近做stm32项目用到一些数据的转换,现贴出来,大家一起讨论:

void float2str(float val, unsigned char Decimal, char *buf) { char fmt[]="%.3f"; fmt[2]=Decimal+0x30; sprintf(buf, fmt, val); } void IntToStr(int dat,char *buf) { sprintf(buf, "%d", dat); } /* 将float类型转为uint16数组的指定位置 value:float类型的值 *arr:数组的指针 startpos:开始的位置 */ void FloatToUint16Array(float value,unsigned short *arr,unsigned short startpos) { unsigned short *p = (uint16_t*)&value ; //把float类型的指针强制转换为uint16_t型 *(arr+startpos+1)=*p++; *(arr+startpos)=*p; } /* uint16数组指定位置转为float arr: 源数组 startpos: 开始位置 返回float */ float ConventToFloat(unsigned short *arr,unsigned short startpos) { float temp; unsigned short *p = (uint16_t*)&temp; //把float类型的指针强制转换为uint16_t型 *p++=*(arr+startpos+1); // pos++; *p=*(arr+startpos); return temp; }

 

转载请注明原文地址: https://www.6miu.com/read-5031269.html

最新回复(0)