问题描述
我正在尝试读取2411字节的二进制文件,我想将所有
文件加载到字符串中。
我做了这个功能:
''--------------------------
公共共享函数Read_bin(ByVal ruta As String)
Dim cadena As String =""
Dim dato As Array
如果File.Exists(ruta)= True则
dato = My.Computer.FileSystem.ReadAllBytes(ruta)
For each dat As Byte in dato
cadena = cadena& Microsoft.VisualBasic.ChrW(dat)
下一页
返回cadena
否则
返回错误
结束如果
结束功能
''--------------------- -----
好吧,我的问题是在最后一个字符串中,字符串中的一些字节不是
是原件,如何纠正?。
提前感谢您的帮助。
Freddy Coal。
Hi, I''m trying to read a binary file of 2411 Bytes, I would like load all
the file in a String.
I make this function for make that:
''--------------------------
Public Shared Function Read_bin(ByVal ruta As String)
Dim cadena As String = ""
Dim dato As Array
If File.Exists(ruta) = True Then
dato = My.Computer.FileSystem.ReadAllBytes(ruta)
For Each dat As Byte In dato
cadena = cadena & Microsoft.VisualBasic.ChrW(dat)
Next
Return cadena
Else
Return "Error"
End If
End Function
''--------------------------
Well, my problem is that in the final string, some bytes in the string not
are the originals, How can correct that?.
Thanks in advance for any help.
Freddy Coal.
推荐答案
您混淆了文本,二进制数据,代码页和
$ b的概念$ b encoding。
当文本存储在文件中时,它被编码为字节,其中每个
字符由一个或多个字节表示。您正在阅读文件
,假设每个字符始终只是一个字节,
可能根本不是真的。
ASCII编码只包含128个字符,你所指的字符表
通常称为扩展ASCII。它不是一个
编码,所以127以上的字符也取决于应用程序使用的代码页
,所以它们可能根本不是显示的表格。
如果你想把数据作为字节来处理,你根本不应该尝试将它转换成字符串。 .NET中的字符串是unicode,所以Char是16
位,而不是8位。
你可能想查看BinaryReader类,如果你希望阅读
二进制数据,它们代表的不仅仅是字节值。
-
G?ran Andersson
_____
这篇关于读取二进制文件,代码更改字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!