十六进制转为float,float转为二进制
2018-06-18 04:12:13来源:未知 阅读 ()
直接贴代码吧,欢迎交流,转载请注明出处,谢谢。
1、头文件:
1 /* 2 * mmath.h 3 * 4 * Created on: Dec 6, 2016 5 * Author: cow 6 */ 7 8 #ifndef MMATH_H_ 9 #define MMATH_H_ 10 #include <stdio.h> 11 #include <stdlib.h> 12 #include <tgmath.h> 13 #include <string.h> 14 15 long FloatTohex(float HEX);//浮点数到十六进制转换 16 17 float BinarytoInt(char *ch,int num);//二进制到整数 18 19 float BinarytoSmallNumber(char *ch,int num);//小数的二进制到小数 20 21 float HextoFloat(char* ch); //传入8位16进制 42F0E666 22 23 24 #endif /* MMATH_H_ */
2、source
/* * mymath.c * * Created on: Dec 6, 2016 * Author: cow */ #include "mymath.h" long FloatTohex(float HEX)//浮点数到十六进制转换1 { return *( long *)&HEX; } float BinarytoInt(char *ch,int num) { int retint = 0,i = 0; for(i = 0;i< num;i++) { retint = retint + (ch[i] == '1' ? 1:0) * pow(2,(num-1-i)); } return (float)retint; } float BinarytoSmallNumber(char *ch,int num) { float retf = 0.0; int i = 0; for(i = 0;i<num;i++) { retf = retf + (ch[i] == '1'?1:0) * pow(2,(-(i + 1)) ) ; } return retf; } float HextoFloat(char* ch) //传入8位16进制 42F0E666 { float returnData = 0.0; int count = 8,i = 0; char binary[32] = {0}; char tmp = '0'; for(i = 0;i<count;i++) { tmp = ch[i]; switch(tmp) { case '0': sprintf(binary+i*4,"%s","0000"); break; case '\0': sprintf(binary+i*4,"%s","0000"); break; case '1': sprintf(binary+i*4,"%s","0001"); break; case '2': sprintf(binary+i*4,"%s","0010"); break; case '3': sprintf(binary+i*4,"%s","0011"); break; case '4': sprintf(binary+i*4,"%s","0100"); break; case '5': sprintf(binary+i*4,"%s","0101"); break; case '6': sprintf(binary+i*4,"%s","0110"); break; case '7': sprintf(binary+i*4,"%s","0111"); break; case '8': sprintf(binary+i*4,"%s","1000"); break; case '9': sprintf(binary+i*4,"%s","1001"); break; case 'A': sprintf(binary+i*4,"%s","1010"); break; case 'B': sprintf(binary+i*4,"%s","1011"); break; case 'C': sprintf(binary+i*4,"%s","1100"); break; case 'D': sprintf(binary+i*4,"%s","1101"); break; case 'E': sprintf(binary+i*4,"%s","1110"); break; case 'F': sprintf(binary+i*4,"%s","1111"); break; case 'a': sprintf(binary+i*4,"%s","1010"); break; case 'b': sprintf(binary+i*4,"%s","1011"); break; case 'c': sprintf(binary+i*4,"%s","1100"); break; case 'd': sprintf(binary+i*4,"%s","1101"); break; case 'e': sprintf(binary+i*4,"%s","1110"); break; case 'f': sprintf(binary+i*4,"%s","1111"); break; default : printf("default \n"); break; } } printf("bin = %s\n",binary); /* * 符号位(1) 指数位(8) 有效数字(23) * */ char symbol = binary[0]; char index[9] = {0}; char effectiveNumber[23] = {0}; memcpy(index,binary+1,8); memcpy(effectiveNumber,binary+9,23); // printf("symbol = %c\n",symbol); // printf("index = %s\n",index); // printf("effectiveNumber = %s\n",effectiveNumber); float indexnum = 0.0,smallNumVal = 0.0,decnum = 0.0; indexnum = BinarytoInt(index,8); //指数位大小 int numofmove = indexnum - 127; //小数点移动值,可能为负数 if(numofmove > 0) //小数点左移组成二进制 { printf("(numofmove) = %d\n",(numofmove)); char * dec = (char *)malloc(( (numofmove) + 2) * sizeof(char)); memset(dec,0,((numofmove) + 2)); sprintf(dec,"%c",'1'); memcpy(dec + 1,effectiveNumber,(numofmove)); printf("dec = %s\n",dec); decnum = BinarytoInt(dec,(numofmove) + 1); printf("decnum = %f\n",decnum); /* //有效数位,去掉整数位才是小数位 */ char* smallNum = (char *)malloc(sizeof(char) * ((23 - (numofmove)) +1) ); memset(smallNum,0,((23 - (numofmove)) + 1));//最后一位\0 //memcpy(smallNum,effectiveNumber + numofmove,(23 - numofmove)); 遇到0停止拷贝 sprintf(smallNum,"%s",effectiveNumber + (numofmove)); printf("smallNum = %s\n",smallNum); smallNumVal = BinarytoSmallNumber(smallNum,(23 - numofmove)); } //整数位是1 else if ( 0 ==numofmove ) { smallNumVal = BinarytoSmallNumber(effectiveNumber,(23 - numofmove)); decnum = 1; } //小数点右移组成二进制,此时要左移小数点,只有小数部分 else { //char* smallNum = (char *)malloc(sizeof(char) * ((23 + (numofmove)) +1) ); char* smallNum = (char*)malloc(sizeof(char) * ( 23 - numofmove +1 )); memset(smallNum,0,((23 - (numofmove)) + 1)); sprintf( (smallNum - numofmove - 1),"%s","1"); //smallNUm[(fabs(numofmove) - 1)] = '1'; sprintf((smallNum - numofmove ),"%s",effectiveNumber); printf("smallNum = %s\n",smallNum); smallNumVal = BinarytoSmallNumber(smallNum,(23 - numofmove)); decnum = 0; } returnData = decnum + smallNumVal;
//free 这里就不写了,懒 if(symbol == '0') //符号位是0 表示正数 { return returnData; } else { return -returnData; } }
3、主函数
/* * main.c * * Created on: Dec 2, 2016 * Author: cow */ #include "mymath.h" int main() { // float f = HextoFloat("42F0E666"); // float f1 = HextoFloat("40B43333"); // float f2 = HextoFloat("3EE66666"); // printf("f = %f\n",f); // printf("f1 = %f\n",f1); // printf("f2 = %f\n",f2); //float flh = 120.45; float flh = -120.45; char hex[9] = {0}; sprintf(hex,"%x",FloatTohex(flh)); printf("hex = %s\n",hex); float f = HextoFloat(hex); printf("f120.45 = %f\n",f); return 0; }
4、执行结果
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:转义字符
- C++ 字符集 2020-03-26
- 十六进制转换 2020-02-23
- 以八进制、十进制、十六进制、布尔值形式输出相关数 2020-02-18
- 【蓝桥杯】十六进制转八进制 2020-02-17
- float和double的区别 2020-01-04
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash