文档库 最新最全的文档下载
当前位置:文档库 › 单精度浮点数转换

单精度浮点数转换

标签(TAG):VB C 单精度浮点数 转换

一、单精度浮点数符合IEEE754标准,32位,前面第一位是符号位,接下来的8位是指数,最后23位是尾数。编程中了解这些就够了,转换方法如下:


二、VB中转换示例:

'VB浮点数转换程序

Option Explicit '利用函数CopyMemory转换

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Dim F As Single '十进制数

Dim A(3) As Byte '16进制浮点数


Private Sub Command1_Click() '转换为十进制数

A(0) = CLng("&H" & Text1(3).Text) '16进制字符转数字

A(1) = CLng("&H" & Text1(2).Text)

A(2) = CLng("&H" & Text1(1).Text)

A(3) = CLng("&H" & Text1(0).Text)

CopyMemory F, A(0), 4 '转换

Text2.Text = F '显示结果

End Sub


Private Sub Command2_Click() '转换为浮点数格式

F = Val(Text2.Text) '十进制字符转数字

CopyMemory A(0), F, 4 '转换

Text1(0).Text = IIf(A(3) > 15, Hex(A(3)), "0" & Hex(A(3))) '显示结果

Text1(1).Text = IIf(A(2) > 15, Hex(A(2)), "0" & Hex(A(2)))

Text1(2).Text = IIf(A(1) > 15, Hex(A(1)), "0" & Hex(A(1)))

Text1(3).Text = IIf(A(0) > 15, Hex(A(0)), "0" & Hex(A(0)))

End Sub


三、C中转换示例:

union sf

{

float f;

unsigned char s[4];

}a;

float m;

unsigned char t[4]

//转换为十进制数

a.s[0]=0x51; //低位在前

a.s[1]=0x06;

a.s[2]=0x9E;

a.s[3]=0x3F;

m=a.f;

//转换为浮点数格式

a.f=m;

t[0]=a.s[0];

t[1]=a.s[1];

t[2]=a.s[2];

t[3]=a.s[3];

相关文档
相关文档 最新文档