首页
社区
课程
招聘
[求助]谁知道这个函数的作用?
发表于: 2005-3-21 16:26 3624

[求助]谁知道这个函数的作用?

2005-3-21 16:26
3624
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啊?
谢谢大家的帮助。

[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!

收藏
免费 0
支持
分享
最新回复 (3)
雪    币: 413
活跃值: (637)
能力值: ( LV9,RANK:170 )
在线值:
发帖
回帖
粉丝
2
130BA38 =19970616   

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进制)
谢谢大家的帮助。
2005-3-21 19:01
0
雪    币: 101
活跃值: (12)
能力值: ( LV12,RANK:210 )
在线值:
发帖
回帖
粉丝
3
'从名称看,似乎是一个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啊?
谢谢大家的帮助。
2005-3-22 01:47
0
雪    币: 33
活跃值: (25)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
 感谢两位的帮助,下面我把主要代码帖出来,让大家有个整体认识。
 
我搜索出一个内存地址后,用函数“ConvertNumberToString”转换19970616为字符串后,在该地址用“WriteProcessMemory”写入转换后的"19970616",但是再用“ReadProcessMemory”读出后,却变为“19923000”了,不知道问题出在哪里?下面是代码,请各路大侠帮助,谢谢。

Private Sub cmdWriteMemory_Click()
  Dim Addr As Long
  Dim value As String
  Dim strvalue As String
  Dim X() As String
  
  strvalue = InputBox("请输入一个数值", "提醒")
  If strvalue = "" Or Not IsNumeric(strvalue) Then Exit Sub
  X = Split(lstResult.List(lstResult.ListIndex), "")
  
  Addr = CLng(Val(Trim(X(0))))
  value = ConvertNumberToString(Val(strvalue))
   
  Call WriteProcessMemory(hProcess, Addr, value, Len(value), 0&)

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

Call ReadProcessMemory(hProcess, lstResult.List(0), ID0, 4, 0&)
Text1(0) = ID0
Call ReadProcessMemory(hProcess, lstResult.List(1), ID1, 4, 0&)
Text1(1) = ID1
Call ReadProcessMemory(hProcess, lstResult.List(2), ID2, 4, 0&)
Text1(2) = ID2
Call ReadProcessMemory(hProcess, lstResult.List(3), ID3, 4, 0&)
Text1(3) = ID3
Call ReadProcessMemory(hProcess, lstResult.List(4), ID4, 4, 0&)
Text1(4) = ID4
Call ReadProcessMemory(hProcess, lstResult.List(5), ID5, 4, 0&)
Text1(5) = ID5
Call ReadProcessMemory(hProcess, lstResult.List(6), ID6, 4, 0&)
Text1(6) = ID6
Call ReadProcessMemory(hProcess, lstResult.List(7), ID7, 4, 0&)
Text1(7) = ID7
Call ReadProcessMemory(hProcess, lstResult.List(8), ID8, 4, 0&)
Text1(8) = ID8
Call ReadProcessMemory(hProcess, lstResult.List(9), ID9, 4, 0&)
Text1(9) = ID9
End Sub
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
2005-3-22 13:55
0
游客
登录 | 注册 方可回帖
返回
//