vb里的二进制转化问题
vb里,怎么样把有符号4个字节的byte型,转化为数字字符串??急,望高手解决!
再线等.....
这个是无符号的转化方法(找来的),帮我看看,有没有问题!
Public Sub DblFromUnInt4(UnInt4() As Byte) as string
iDbl As Double
Dim Ii As Integer
iDbl = 0#
For Ii = 0 To 3
iDbl = iDbl + UnInt4(Ii) * 16 ^ (6 - Ii * 2)
Next Ii
DblFromUnInt4=CStr(jieguo)
End Sub
[解决办法]
首先,函数定义错误。
另外,算法好象不对。
还有,返回的地方变量错误。
Option Explicit
Sub main()
Dim aa(3) As Byte
aa(0) = 1: aa(1) = 1: aa(2) = 1: aa(3) = 1
Debug.Print DblFromUnInt4(aa)
End Sub
Public Function DblFromUnInt4(UnInt4() As Byte) As String
Dim iDbl As Double
iDbl = 0#
iDbl = iDbl + UnInt4(0) * 16 ^ 3
iDbl = iDbl + UnInt4(1) * 16 ^ 2
iDbl = iDbl + UnInt4(2) * 16
iDbl = iDbl + UnInt4(3)
DblFromUnInt4 = CStr(iDbl)
End Function
[解决办法]
顶一下,当值为负数时,cangwu_lee(橙子) 的转化方法不对了吧
======================================================
cangwu_lee(橙子)的方法是对的,但他只提供了第一步。具体是这样做的:
1、用CopyMemory将tByte()数组复制到tlong(一个long类型)
2、判断tLong是正的还是负的,如果是正数或者0直接用一个tDouble(Double类型)=tLong。
3、如果tLong是负数,想转换为大于2^31的无符号整数。
Dim tDouble As Double
Dim tLong As Long
Dim tLong2 As Long
tLong = -1#
tDouble = ((tLong And &H7FFFFFFF)) + 2 ^ 31
Text1.Text = tDouble