2024年6月15日发(作者:)

1 Matlab程序

clear

clc

%16 进制转浮点数

a = 'C8BE3700'

b = typecast(uint32(hex2dec(a)),'single')

%浮点数转16进制 方法一

c= dec2hex(typecast(b,'uint32'))

%浮点数转16进制 方法二

sprintf('%tx',single(b))

2 VBA程序

Private Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination

As Any, Source As Any, ByVal Length As Long)

'==========================================================

=====================

' User-defined data types

' (Necessary because that's the only way LSET works)

'==========================================================

=====================

Type uLng: l As Long: End Type

Type uFlt: f As Single: End Type

Public Function Hex2Float(s As String) As Single

Dim b() As Byte

Dim i As Integer

Dim n As Integer

' s = "4354E598"

ReDim b(Len(s) 2 - 1)

For i = UBound(b) To 0 Step -1

b(n) = Val("&H" & Mid(s, i * 2 + 1, 2))

n = n + 1

Next

CopyMemory Hex2Float, b(0), 4

End Function

' For i = UBound(b) To 0 Step -1

' b(n) = Val("&H" & Mid(s, i * 2 + 1, 2))

' n = n + 1

' Next

' CopyMemory v, b(0), 4

'==========================================================

=====================

' Functions for Singles (church dances)

'==========================================================

=====================

Function Float2Hex(s As Single) As String

' shg 2008-0919

' Returns the conversion of float s to a hex string

Const sPad As String = "00000000"

Dim uf As uFlt

Dim ul As uLng

uf.f = s

LSet ul = uf

Float2Hex = Right(sPad & Hex(ul.l), 8)

End Function

3 Python程序

import ctypes

def h2f(s):

cp = r(ctypes.c_long(s))

fp = (cp, R(ctypes.c_float))

return

def f2h(s):

fp = r(ctypes.c_float(s))

cp = (fp, R(ctypes.c_ulong))

return hex()

print(h2f(0xC8BE3700))

print(f2h(-389560.0))