Private Function ConvertNumberToString(number As Double) As String
If number < 256 Then ConvertNumberToString = Chr(number): Exit Function
If number < 65536 Then
ConvertNumberToString = Chr(number And 255) & Chr((number And 65280) / 256)
Exit Function
End If
b4 = number And 255
number = Int(number / 256)
b3 = number And 255
number = Int(number / 256)
b2 = number And 255
number = Int(number / 256)
b1 = number And 255
number = Int(number / 256)
ConvertNumberToString = Chr(b4) & Chr(b3) & Chr(b2) & Chr(b1)
End Function
哪们朋友告诉我上面函数的作用和它的BUG出在什么地方?
19970616 and 255 返回值为什么是56啊?
谢谢大家的帮助。
Private Function ConvertNumberToString(number As Double) As String
If number < 256 Then ConvertNumberToString = Chr(number): Exit Function
%% 是不是该加一个End if呢
If number < 65536 Then
ConvertNumberToString = Chr(number And 255) & Chr((number And 65280) / 256)
Exit Function
End If
%% 130BA38=19970616
b4 = number And 255 %%38H
number = Int(number / 256) %%130BA
b3 = number And 255 %%BA
number = Int(number / 256) %%130H
b2 = number And 255 %%30H
number = Int(number / 256) %%01
b1 = number And 255 %%01
number = Int(number / 256) %%00 可以不加这一句吧
ConvertNumberToString = Chr(b4) & Chr(b3) & Chr(b2) & Chr(b1)
%% 这个顺序不对吧,应该是B1,B2,B3,B4的吧,我不懂VB,不知道串的合成关系。
End Function
哪们朋友告诉我上面函数的作用和它的BUG出在什么地方?
19970616 and 255 返回值为什么是56啊? B4=38H=56D(10进制)
谢谢大家的帮助。
'从名称看,似乎是一个double转字符串的函数 --2
'综观整个程序,作者似乎把number当一个dword来处理.
'作者意图似乎输入 0x1234abcd 输出 "1234abcd" --1
'不知道这里的double是否写错误了呢?
'补充: 如果目的1,可以用函数 Hex(&h1234abcd&)实现
'如果目的2, 可以用函数Cstr(34.3333)实现
Private Function ConvertNumberToString(number As Double) As String
'这一句证明作者对chr函数理解错误. 这里说明2个问题
'对于单字节的数字想直接显示,但这个chr使用错误.
'作者可能认为 chr(200) = printf("%d",200) 但实际
'chr(200) = printf("%c", 200); 不知这样还有意义不?
If number < 256 Then ConvertNumberToString = Chr(number): Exit Function
'--下面同理, 不过可以用辗转相除法. 不需要这么复杂.
'另 (number And 65280) / 256 = number \ 256
'vb中有整除运算符,即便没有,也是用Fix(number / 256)
If number < 65536 Then
ConvertNumberToString = Chr(number And 255) & Chr((number And 65280) / 256)
Exit Function
End If
'这里的b1,b2,b3,b4都没有定义,应该在程序开头
'dim b1 as byte, b2 as byte, b3 as byte, b4 as byte
'这里自动变成variant类型.如果有optional explicit申明的话
'则程序报错
b4 = number And 255
number = Int(number / 256)
b3 = number And 255
number = Int(number / 256)
b2 = number And 255
number = Int(number / 256)
b1 = number And 255
number = Int(number / 256)
'同样高字节->低字节的顺序为 b1 b2 b3 b4
'下面顺序反了.
ConvertNumberToString = Chr(b4) & Chr(b3) & Chr(b2) & Chr(b1)
End Function
'and 按位与操作运算符. 注意这个数最低字节
'(199700616 mod 256)看看
哪们朋友告诉我上面函数的作用和它的BUG出在什么地方?
19970616 and 255 返回值为什么是56啊?
谢谢大家的帮助。
End Sub
Private Sub cmdReadMemory_Click()
On Error Resume Next
Dim ID0 As Long, ID1 As Long, ID2 As Long, ID3 As Long, ID4 As Long
Dim ID5 As Long, ID6 As Long, ID7 As Long, ID8 As Long, ID9 As Long
If number < 256 Then ConvertNumberToString = Chr(number): Exit Function
If number < 65536 Then
ConvertNumberToString = Chr(number And 255) & Chr((number And 65280) / 256)
Exit Function
End If
b4 = number And 255
number = Int(number / 256)
b3 = number And 255
number = Int(number / 256)
b2 = number And 255
number = Int(number / 256)
b1 = number And 255
number = Int(number / 256)
ConvertNumberToString = Chr(b4) & Chr(b3) & Chr(b2) & Chr(b1)
End Function