几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量

几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量 (http://www.dimcax.com/hust/index.php)
-   ObjectARX(VB.NET/C#) (http://www.dimcax.com/hust/forumdisplay.php?f=176)
-   -   [vba]如何对二进制字符转化为数值,再将数值转化为二进制字符,重新写到二进制文件中 (http://www.dimcax.com/hust/showthread.php?t=9066)

yang686526 2009-04-20 02:52 PM

[vba]如何对二进制字符转化为数值,再将数值转化为二进制字符,重新写到二进制文件中
 
[vba]如何对二进制字符转化为数值,再将数值转化为二进制字符,重新写到二进制文件中
www.dimcax.com
[vba]如何对二进制字符转化为数值,再将数值转化为二进制字符,重新写到二进制文件中
如何对二进制变量中的二进制字符逐一提取进行转化为数值,然后再将数值转化为二进制字符,重新写到二进制文件中。
下面是我测试的过程。
在测试一:读取的二进制变量在degub窗口中显示后,重新输出到另一文件中,输出的文件正确。
在测试二:输出文件已经变化,格式已经变化。
在测试三:可以很清楚看到数值与结果的变化。
我该如何操作才能获得正确的#3文件,请大伙帮帮忙!!!
const blocksize = 4096
public sfilename as string
public dfilename as string
public newfilename as string
private sub savetodf()
'定义数据块数组
dim bytedata() as byte
'定义数据块个数
dim numblocks as long
dim filelength as long
'定义剩余字节长度
dim leftover as long
dim sourcefile as long
dim diskfile as long
dim i as long
'判断文件是否存在
if dir(sfilename) <> "" then
sourcefile = freefile
diskfile = freefile
'打开二进制文件
'open sfilename for binary access read as sourcefile
'open dfilename for binary access write as diskfile
open sfilename for binary access read as #1
open dfilename for binary access write as #2
filelength = lof(1)
'判断文件是否空
if filelength = 0 then
close #1
msgbox sfilename & "文件无内容,请重新指定文件!", vbexclamation, "注意"
else
'得到数据块的个数
numblocks = filelength \ blocksize
'得到剩余字节数
leftover = filelength mod blocksize
'fld.value = null

debug.print "filelength=" & filelength, "numblocks=" & numblocks, "leftover =" & leftover
redim bytedata(blocksize)
for i = 1 to numblocks '测试一
get #1, , bytedata()
'debug.print bytedata()
put #2, , bytedata()
call newfile(bytedata(), 4096)
doevents
next i
'将剩余数据写入fld
redim bytedata(leftover)
get #1, , bytedata()
'debug.print bytedata()
put #2, , bytedata()
call newfile(bytedata(), leftover)
'fld.appendchunk bytedata()
close #1
close #2
end if
else
msgbox "文件不存在,请重新指定文件!", vbexclamation, "注意"
end if
end sub
private function getfilename() as string

commondialog1.cancelerror = true
on error goto cancelerr
commondialog1.filter = "所有文件(*.*)|*.*"
commondialog1.showopen
getfilename = commondialog1.filename
exit function
cancelerr:
getfilename = ""

end function
private sub command1_click()
sfilename = getfilename
label1.caption = sfilename
command2.enabled = true
command1.enabled = false

end sub
private sub command2_click()
'我想在这里通过加密手段处理一下数据,必须把二进制字符变为相应的数值,然后加密,解密,输出到新的文件中。请各位指点!!!
dfilename = getfilename
label2.caption = dfilename
newfilename = app.path & "/123." & right(dfilename, 3)
open newfilename for binary access write as #3 '测试二
call savetodf
close #3
command1.enabled = true
command2.enabled = false
end sub
private sub form_load()
command1.enabled = true
command2.enabled = false
close all
end sub
public sub newfile(data() as byte, flenght as long)
'求救!!!
dim strnum as long
dim newstr as string
dim ii as long
for ii = 1 to flenght
strnum = ascw(data(ii))
newstr = str(strnum)
put #3, , newstr
debug.print data(ii) & "-" & strnum & "-" & newstr '测试三
next ii
end sub


所有的时间均为北京时间。 现在的时间是 12:01 PM.