vb中如何将二进制数转换为十进制数考虑小数和负号
答案:2 悬赏:0 手机版
解决时间 2021-04-07 11:59
- 提问者网友:练爱
- 2021-04-06 17:22
vb中如何将二进制数转换为十进制数考虑小数和负号
最佳答案
- 五星知识达人网友:琴狂剑也妄
- 2021-04-06 18:00
搞清楚二进制和十进制关系就可以了,二进制逢二进一,只有0和1。
K为二进制数里某N位数字,则转成十进位则是K乘以2的N次方,小数则为K乘以2的-N次方。
K为二进制数里某N位数字,则转成十进位则是K乘以2的N次方,小数则为K乘以2的-N次方。
全部回答
- 1楼网友:玩家
- 2021-04-06 19:31
弄的很复杂,不知道哪位有简单的方法?
二进制转换为小数值没法直接处理,土办法有一个,通过二进制文件来转换,如把Double数值写到二进制文件,再用8个字节的数组读取出来。
试试下面两函数
DecToBits 把小数值转换为一二进制字符串
BitsToDec 再把二进制串转换为小数,能还愿,将就用吧。
'数值转换为二进制字符串
Public Function DecToBits(ByVal d As Double) As String
Dim id As Integer
id = FreeFile
Open GetTempfile For Binary As #id
Put #id, 1, d
Dim bt() As Byte
ReDim bt(1 To Len(d))
Get #id, 1, bt
Close #id
Dim i As Integer
Dim b As Byte
Dim btL As Integer
btL = UBound(bt)
For i = 1 To btL 2 '数字高字节在右边,把它倒过来
b = bt(i)
bt(i) = bt(btL - i + 1)
bt(btL - i + 1) = b
Next
DecToBits = BytesToBits(bt)
End Function
'二进制字符串转换为数值
Public Function BitsToDec(ByVal bitString As String) As Double
If bitString Like "*[!01]*" Then
Err.Raise 5
Else
Dim id As Integer
id = FreeFile
Open GetTempfile For Binary As #id
Dim bt() As Byte
bitString = Left(bitString, 1) & String(64 - Len(bitString), "0") & Mid(bitString, 2)
bt = BitsToBytes(bitString)
Dim btL As Integer
btL = UBound(bt)
Dim b As Byte
Dim i As Integer
For i = 1 To btL 2 '数字高字节在右边,把它倒过来
b = bt(i)
bt(i) = bt(btL - i + 1)
bt(btL - i + 1) = b
Next
Put #id, 1, bt
Dim d As Double
Get #id, 1, d
Close #id
BitsToDec = d
End If
End Function
'-------------------------------------------------------------------------
'-------------------------------------------------------------------------
'字节数组转换为二进制字符串
Public Function BytesToBits(bts() As Byte) As String
Dim i As Integer
Dim bitString As String
For i = LBound(bts) To UBound(bts)
bitString = bitString & ByteToBits(bts(i))
Next
BytesToBits = bitString
End Function
'单个字节转换为二进制字符串
Public Function ByteToBits(ByVal bt As Byte) As String
Dim i As Integer
Dim pt As Byte
Dim s As String
s = String(8, "0")
For i = 0 To 7
pt = 2 ^ i
If bt And pt Then
Mid(s, 8 - i, 1) = "1"
End If
Next
ByteToBits = s
End Function
'-------------------------------------------------
'二进制字符串转换为字节数组
Public Function BitsToBytes(ByVal bitString As String) As Byte()
If bitString Like "*[!01]*" Then
Err.Raise 5
Else
Dim strM As Integer
strM = Len(bitString) Mod 8
If strM > 0 Then
bitString = Left(bitString, 1) & String(8 - strM, "0") & Mid(bitString, 2)
End If
'bitString = StrReverse(bitString)
Dim bt() As Byte
ReDim bt(1 To Len(bitString) 8)
Dim i As Integer
For i = 1 To UBound(bt)
bt(i) = BitsToByte(Mid(bitString, i * 8 - 7, 8))
Next
BitsToBytes = bt
End If
End Function
'8位二进制字符串转换为Byte类型
Public Function BitsToByte(ByVal bits As String) As Byte
If Not bits Like "*[!01]*" And Len(bits) <= 8 Then
Dim i As Integer
Dim bt As Byte
Dim bL As Integer
bL = Len(bits)
For i = 1 To bL
If Mid(bits, i, 1) = "1" Then
bt = bt + 2 ^ (bL - i)
End If
Next
BitsToByte = bt
Else
Err.Raise 5
End If
End Function
'----------------------------------------------------------------
'临时文件名
Public Function GetTempfile() As String
Static TempFile As String
If TempFile = Empty Then
Dim fso As Object
Set fso = CreateObject("Scripting.Filesystemobject")
TempFile = fso.GetSpecialFolder(2) & "" & fso.GetTempName
End If
GetTempfile = TempFile
End Function
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯