浮点数格式与IEEE-754标准(32)有关,长度32位。四个字节的浮点数据传送顺序为先低字节后高字节,即传送顺序为:先低字节D7~D0,接着D15~D8,然后D23~D16,最后高字节D31~D24,最终作为分成8个ASCII码传送。浮点数格式如下:
首先将16进制转化为2进制:
Code
1CString Utility::HexCharToCString(char chHex)
2{
3 switch(chHex)
4 {
5 case '0':
6 return "0000";
7 case '1':
8 return "0001";
9 case '2':
10 return "0010";
11 case '3':
12 return "0011";
13 case '4':
14 return "0100";
15 case '5':
16 return "0101";
17 case '6':
18 return "0110";
19 case '7':
20 return "0111";
21 case '8':
22 return "1000";
23 case '9':
24 return "1001";
25
26
27 case 'A':
28 case 'a':
29 return "1010";
30 case 'B':
31 case 'b':
32 return "1011";
33 case 'C':
34 case 'c':
35 return "1100";
36 case 'D':
37 case 'd':
38 return "1101";
39 case 'E':
40 case 'e':
41 return "1110";
42 case 'F':
43 case 'f':
44 return "1111";
45 default:
46 return "0000";
47
48 }
49
50}
再将2进制数按浮点数规则进行计算:
1CString Utility::HexCharToCString(char chHex)
2{
3 switch(chHex)
4 {
5 case '0':
6 return "0000";
7 case '1':
8 return "0001";
9 case '2':
10 return "0010";
11 case '3':
12 return "0011";
13 case '4':
14 return "0100";
15 case '5':
16 return "0101";
17 case '6':
18 return "0110";
19 case '7':
20 return "0111";
21 case '8':
22 return "1000";
23 case '9':
24 return "1001";
25
26
27 case 'A':
28 case 'a':
29 return "1010";
30 case 'B':
31 case 'b':
32 return "1011";
33 case 'C':
34 case 'c':
35 return "1100";
36 case 'D':
37 case 'd':
38 return "1101";
39 case 'E':
40 case 'e':
41 return "1110";
42 case 'F':
43 case 'f':
44 return "1111";
45 default:
46 return "0000";
47
48 }
49
50}
Code
1CString Utility::FloatIndexOfCharArray(int index,char *chArr)
2{
3 CString str="";
4 CString *p=new CString[32];
5 int E=0;
6 int M=0;
7 double Result=0;
8 for(int i=0;i<8;i++)
9 {
10 str+=HexCharToCString(*(chArr+index+i));
11 }
12
13 int *c=new int[32];
14 for(int i=0;i<str.GetLength();i++)
15 {
16 p[i]=str.GetAt(i);
17 }
18 for(int j=0;j<str.GetLength();j++)
19 {
20 c[j]=atoi(p[j]);
21 }
22 E=((*(c+1))*128+(*(c+2))*64+(*(c+3))*32+(*(c+4))*16+(*(c+5))*6+(*(c+6))*4+(*(c+7))*2+(*(c+8))*1)-127;
23
24 M=(*(c+9))*4194304+(*(c+10))*2097152+(*(c+11))*1048576+(*(c+12))*524288+(*(c+13))*262144+(*(c+14))*131072+(*(c+15))*65536+(*(c+16))*32768+(*(c+17))*16384+(*(c+18))*8192
25 +(*(c+19))*4096+(*(c+20))*2048+(*(c+21))*1024+(*(c+22))*512+(*(c+23))*256+(*(c+24))*128+(*(c+25))*64+(*(c+26))*32+(*(c+27))*16+(*(c+28))*8+(*(c+29))*4+(*(c+30))*2+(*(c+31));
26
27 Result=(1+M*(pow((double)2,-23)))*pow((double)2,E);
28 if(c[0]==1)
29 {
30 Result=Result*(-1);
31 }
32 str.Format("%.2f",Result);
33 delete[]p;
34 delete[]c;
35 return str;
36
37
38}
39
1CString Utility::FloatIndexOfCharArray(int index,char *chArr)
2{
3 CString str="";
4 CString *p=new CString[32];
5 int E=0;
6 int M=0;
7 double Result=0;
8 for(int i=0;i<8;i++)
9 {
10 str+=HexCharToCString(*(chArr+index+i));
11 }
12
13 int *c=new int[32];
14 for(int i=0;i<str.GetLength();i++)
15 {
16 p[i]=str.GetAt(i);
17 }
18 for(int j=0;j<str.GetLength();j++)
19 {
20 c[j]=atoi(p[j]);
21 }
22 E=((*(c+1))*128+(*(c+2))*64+(*(c+3))*32+(*(c+4))*16+(*(c+5))*6+(*(c+6))*4+(*(c+7))*2+(*(c+8))*1)-127;
23
24 M=(*(c+9))*4194304+(*(c+10))*2097152+(*(c+11))*1048576+(*(c+12))*524288+(*(c+13))*262144+(*(c+14))*131072+(*(c+15))*65536+(*(c+16))*32768+(*(c+17))*16384+(*(c+18))*8192
25 +(*(c+19))*4096+(*(c+20))*2048+(*(c+21))*1024+(*(c+22))*512+(*(c+23))*256+(*(c+24))*128+(*(c+25))*64+(*(c+26))*32+(*(c+27))*16+(*(c+28))*8+(*(c+29))*4+(*(c+30))*2+(*(c+31));
26
27 Result=(1+M*(pow((double)2,-23)))*pow((double)2,E);
28 if(c[0]==1)
29 {
30 Result=Result*(-1);
31 }
32 str.Format("%.2f",Result);
33 delete[]p;
34 delete[]c;
35 return str;
36
37
38}
39
最后的str为计算好的值!鄙视下自己竟然忘记了输出函数:::afxmessageBox("Hello world!");我写的是C++的mfc程序!