栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c语言char和float相互转换

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c语言char和float相互转换

转换代码如下

union CharFloat{
	float numeric;
	unsigned char ascii[4];
};
float charTofloat(unsigned char *str) {
	CharFloat value;
	value.ascii[0] = str[0];
	value.ascii[1] = str[1];
	value.ascii[2] = str[2];
	value.ascii[3] = str[3];
	return value.numeric;
}
void floatochar(char* returnData, float numeric) {
	CharFloat value;
	int i = 0;
	value.numeric = numeric;
	returnData[i++] = value.ascii[3];
	returnData[i++] = value.ascii[2];
	returnData[i++] = value.ascii[1];
	returnData[i++] = value.ascii[0];
	for(int j=0;j<4;j++)
		printf("%2x,",value.ascii[j]);
}

要得到1位小数的话,整数不能高于5位。

要得到2位小数的话,整数不能高于4位

要得到3位小数的话,整数不能高于3位

要得到4位小数的话,整数不能高于2位

要得到5位小数的话,整数不能高于1位

要得到6位小数的话,整数不能高于0位

小数最多七位其实是0.1000000

同理可以计算double的精确度整数最多16位,小数最多16位(0.1000000000000000)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/385028.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号