找回密码
 加入
搜索
查看: 7718|回复: 8

[AU3基础] 二进制数据替换问题?[已解决]

  [复制链接]
发表于 2010-6-24 10:27:06 | 显示全部楼层 |阅读模式
本帖最后由 newuser 于 2010-6-29 07:53 编辑
;文本内容只有一行: 10.600.58.32,我需要把它变成10.600.85.23
Dim $File=FileOpen(@DesktopDir&"\test.txt",0+16) ;0是只读模式,16表示强制使用二进制(字节)模式
;检查打开的文件是否可读
If $File = -1 Then
        MsgBox(0,"error","无法打开文件")
EndIf
$FileContent=FileRead($File)
MsgBox(0,"显示被读取文件的二进制形式",$FileContent)
FileClose($File) ;关闭文件句柄
;$z=Hex(Asc("z"),2)
;$y=Hex(Asc("y"),2)
;$58=Hex(Asc("58"),2)
$58=Hex(Asc(58),2)
$32=Hex(Asc(32),2)
$85=Hex(Asc(85),2)
$23=Hex(Asc(23),2)
;$FileContentReplace=StringReplace($FileContent,$z,$y) ;把文件中的字母z换成y
$FileContentReplace1=StringReplace($FileContent,$58,$85) ;把文件中的数字58换成85
$FileContentReplace2=StringReplace($FileContent,$32,$23) ;把文件中的数字32换成23

$File=FileOpen(@DesktopDir&"\test.txt",2+16) ;2表示不删除原文件内容写入模式,16表示二进制的写入模式
;FileWrite($File,$FileContentReplace) ;向文件写入替换后的内容
FileWrite($File,$FileContentReplace1)
FileWrite($File,$FileContentReplace2)
FileClose($File)
学习CroosDoor老师教程又遇到了问题,请大家帮助解决

评分

参与人数 1金钱 +10 收起 理由
afan + 10 感谢主动将修改帖子分类为[已解决],请继续 ...

查看全部评分

发表于 2010-6-24 11:46:37 | 显示全部楼层
使用二进制是因为特殊需要?还是?
$File = @ScriptDir & '\test.txt'
$Text = FileRead($File)
$open = FileOpen($File, 2)
FileWrite($File, StringReplace($Text, '10.600.58.32', '10.600.85.23'))
FileClose($open)

评分

参与人数 1威望 +1 收起 理由
afan + 1

查看全部评分

 楼主| 发表于 2010-6-24 13:42:23 | 显示全部楼层
本帖最后由 newuser 于 2010-6-24 13:54 编辑

回复 2# 破帽遮颜
成功了! 但是二进制模式如何替换呢?
因为在二进制文件中,内容可能很多行,而且要替换的IP地址位置也不一定就位于行首或行尾,就不能简单的fileread()或filereadline()了.
发表于 2010-6-25 17:12:28 | 显示全部楼层
只是追加二进制修改后的文件
;文本内容只有一行: 10.600.58.32,我需要把它变成10.600.85.23
Dim $File=FileOpen(@DesktopDir&"\test.txt",0+16) ;0是只读模式,16表示强制使用二进制(字节)模式
;检查打开的文件是否可读
If $File = -1 Then
        MsgBox(0,"error","无法打开文件")
EndIf
$FileContent=FileRead($File)
FileClose($File) ;关闭文件句柄
;MsgBox(0,"显示被读取文件的二进制形式",$FileContent)
$Search=StringTrimLeft(Binary("58.32"),2)
$Replace=StringTrimLeft(Binary("85.23"),2)
;MsgBox(0,"要查找的二进制代码",$Search)
;MsgBox(0,"要替换的二进制代码",$Replace)
$re=StringReplace($FileContent,$Search,$Replace)
;MsgBox(0,0,BinaryToString($re))
FileWriteLine(@DesktopDir&"\test.txt",BinaryToString($re))

评分

参与人数 1威望 +1 收起 理由
afan + 1

查看全部评分

发表于 2010-6-25 17:36:05 | 显示全部楼层
修改文件的
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>

Global $sFile, $hFile, $sText, $nBytes, $tBuffer


 $sFile = @DesktopDir & '\test.txt'
 $sText = Binary(FileRead($sFile))
 $tBuffer = DllStructCreate("byte[" & StringLen($sText) & "]")
 $Search=StringTrimLeft(Binary("58.32"),2)
 $Replace=StringTrimLeft(Binary("85.23"),2)
 $re=BinaryToString(StringReplace($sText,$Search,$Replace))
 ;MsgBox(0,0,$re)
 DllStructSetData($tBuffer, 1, $re)
 $hFile = _WinAPI_CreateFile($sFile, 2)
 _WinAPI_WriteFile($hFile, DllStructGetPtr($tBuffer), StringLen($re), $nBytes)
 _WinAPI_CloseHandle($hFile)

评分

参与人数 1金钱 +20 收起 理由
afan + 20

查看全部评分

 楼主| 发表于 2010-6-28 15:21:24 | 显示全部楼层
回复 5# 3mile

没有换?
发表于 2010-6-28 18:00:11 | 显示全部楼层
回复 6# newuser
5楼代码经测试没有问题。
 楼主| 发表于 2010-6-29 07:52:48 | 显示全部楼层
本帖最后由 newuser 于 2010-6-29 08:16 编辑

谢谢,是我的问题.目前能力有限,还是不能理清思路,继续努力吧!
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>

Global $sFile, $hFile, $sText, $nBytes, $tBuffer


$sFile = @DesktopDir & '\test.txt' ;要读取文件的路径
$sText = Binary(FileRead($sFile)) ;读文件内容并将内容转换成二进制
$tBuffer = DllStructCreate("byte[" & StringLen($sText) & "]") ;创建1个C/C++样式数据结构供dllcall()使用
;语法:DllStructCreate ( "数据结构" [,指针] )
$Search=StringTrimLeft(Binary("58.32"),2) ;将文本中准备替换的内容转换成二进制并从字符串左开始删除指定数量的字符
$Replace=StringTrimLeft(Binary("85.23"),2)
$re=BinaryToString(StringReplace($sText,$Search,$Replace)) ;实现替换过程并将替换后的内容转换成字符
;MsgBox(0,0,$re)
DllStructSetData($tBuffer, 1, $re) ;设置数据结构中部分元素的数据。语法:DllStructSetData ( 数据结构, 元素, 值 [, 索引 ] )
$hFile = _WinAPI_CreateFile($sFile, 2) ;为其它设备创建或者打开一个文件.
_WinAPI_WriteFile($hFile, DllStructGetPtr($tBuffer), StringLen($re), $nBytes) ;写入数据到文件指定位置
_WinAPI_CloseHandle($hFile) ;关闭打开文件的句柄
回复 7# 3mile
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-17 14:58 , Processed in 0.090144 second(s), 27 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表