Joke Collection Website - Blessing messages - How to convert single-precision floating-point numbers into decimal numbers

How to convert single-precision floating-point numbers into decimal numbers

1. Single-precision floating-point number conforms to IEEE754 standard, with 32 bits, the first bit in front is the sign bit, the last 8 bits are the exponent, and the last 23 bits are the mantissa. It is enough to know this in programming, and the conversion method is as follows:

Second, the transformation example in VB:

VB floating-point number conversion program

Use the function CopyMemory for OptionExplicit'' conversion.

PrivateDeclausebcopymorylib "kernel32" alias "rtlmovemory" (Destination Asany, SourceAsAny, ByValLengthAsLong).

DimFAsSingle'' decimal number

Ma Di (3) asbyte' 16 hexadecimal floating-point number

The private subcommand 1 _ click ()' is converted to decimal numbers.

A (0) = clng ("&h" & text 1 (3)。 Text)' 16 hexadecimal characters to numbers.

a( 1)= CLng(" & H " & text 1(2)。 Text)

a(2)= CLng(" & H " & text 1( 1)。 Text)

A(3)=CLng("&H"&Text 1(0)。 Text)

CopyMemoryF, A(0), 4' conversion

Text2.text = f' displays the result.

End joint

PrivateSubCommand2_Click ()' ()' is converted to floating-point number format.

F = val (text2.text)' Decimal characters to numbers.

CopyMemoryA(0), f, 4' conversion

Text 1 (0)。 Text = IIF (a (3) > 15, hexadecimal (a (3)), "0" & hexadecimal (A(3))' displays the results.

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

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

Text 1(3)。 Text=IIf(A(0)> 15,Hex(A(0))," 0"&Hex(A(0)))

End joint

Extended data:

Examples of transformation in C language:

Trade union f

{

floatf

unsigned chars[4];

} a;

floatm

Unsigned chart [4]

//Convert to decimal number

a . s[0]= 0x 5 1; //Low priority

a . s[ 1]= 0x 06;

a . s[2]= 0x9E;

a . s[3]= 0x3F;

m = a.f

//Convert to floating-point number format

a . f = m;

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

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

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

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