VBA - 将 15 MB 二进制文件读取到 var
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8377574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:36:52 来源:igfitidea点击:
VBA - Read 15 MB binary file to var
提问by bpelhos
Is it possible in Excel VBA to read a binary file to a variable?
是否可以在 Excel VBA 中将二进制文件读取到变量?
A function similar to:
一个类似于:
function bin2var(filename as String) As XXXX
And also, is it possible to do the reverse operation?
而且,是否可以进行相反的操作?
function var2bin(filename as String,data as XXXX)
回答by Boann
Function bin2var(filename As String) As String
Dim f As Integer
f = FreeFile()
Open filename For Binary Access Read Lock Write As #f
bin2var = Space(FileLen(filename))
Get #f, , bin2var
Close #f
End Function
Sub var2bin(filename As String, data As String)
Dim f As Integer
f = FreeFile()
Open filename For Output Access Write Lock Write As #f
Print #f, data;
Close #f
End Sub
回答by bpelhos
Rewriting Boann's code as it writes a new line (2 byte) at the end of the output file.
重写 Boann 的代码,因为它在输出文件的末尾写了一个新行(2 个字节)。
Function bin2var(filename As String) As String
Dim f As Integer
f = FreeFile()
Open filename For Binary Access Read Lock Write As #f
bin2var = Space(FileLen(filename))
Get #f, , bin2var
Close #f End Function
Sub var2bin(filename As String, data As String)
Dim f As Integer
f = FreeFile()
Open filename For Binary Access Write As #f
Put #f, , data
Close #f
End Sub