读取二进制文件,代码更改字节 [英] Reading Binary file, the code change bytes

查看:68
本文介绍了读取二进制文件,代码更改字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取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.

推荐答案

可能是字符编码问题。您假设每个字节编码一个

字符,这是不必要的。这个二元是什么文件应该是

是什么?


如果这是一个真的话二进制文件应该可以作为
字节的数组处理。如果这是文本,它应该可以用正确的字符

编码来阅读。


看看周围
http://msdn2.microsoft.com/en-us /lib...ng(VS.80).aspx


Patrice


Freddy Coal < fr ******** @gmail.withoutspan.comaécritdansle message de

news:on **************** @ TK2MSFTNGP04.phx.gbl ...
Likely a character encoding issue. You assume that each byte encode a single
character which is not necessaryly true. What this "binary" file is supposed
to be ?

If this is a "true" binary file it should be likely processed as an array of
byte. If this is text, it should be likely read with the proper character
encoding.

See around
http://msdn2.microsoft.com/en-us/lib...ng(VS.80).aspx

Patrice

"Freddy Coal" <fr********@gmail.withoutspan.coma écrit dans le message de
news: On****************@TK2MSFTNGP04.phx.gbl...

我正在尝试读取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.



Patrice,文件中的每4个字节都是我的号码。我认为每个
字节都是一个字符,因为1个字符有8位,而ASCII有255个字符;

我需要获取字符,接受它,加入4个字节得到这个号码。

http://msdn2.microsoft.com/en-us/lib...8t(VS.80).aspx
http://msdn2.microsoft.com/en-us/lib...28(VS .80).aspx


" Patrice" < http://www.chez.com/scribe/wrote in message

news:uM ************** @ TK2MSFTNGP05.phx.gbl .. 。
Patrice, each 4 bytes in the file are a number for my. I supose that each
byte is a char, because 1 char have 8bit, and the ASCII have 255 characters;
I need obtain the char, take that, join 4 bytes and get the number.

http://msdn2.microsoft.com/en-us/lib...8t(VS.80).aspx
http://msdn2.microsoft.com/en-us/lib...28(VS.80).aspx

"Patrice" <http://www.chez.com/scribe/wrote in message
news:uM**************@TK2MSFTNGP05.phx.gbl...

可能是字符编码问题。您假设每个字节编码一个

单个字符,这是不必要的。这个二元是什么文件是

应该是什么?


如果这是一个真的话二进制文件应该可以作为数组处理

的字节。如果这是文本,它应该可以用正确的

字符编码来阅读。


看看周围
http://msdn2.microsoft.com/en-us /lib...ng(VS.80).aspx


Patrice


Freddy Coal < fr ******** @gmail.withoutspan.comaécritdansle message

de news:on **************** @ TK2MSFTNGP04.phx.gbl ...
Likely a character encoding issue. You assume that each byte encode a
single character which is not necessaryly true. What this "binary" file is
supposed to be ?

If this is a "true" binary file it should be likely processed as an array
of byte. If this is text, it should be likely read with the proper
character encoding.

See around
http://msdn2.microsoft.com/en-us/lib...ng(VS.80).aspx

Patrice

"Freddy Coal" <fr********@gmail.withoutspan.coma écrit dans le message
de news: On****************@TK2MSFTNGP04.phx.gbl...

>我正在尝试读取2411字节的二进制文件,我想加载所有
字符串中的文件。

我为此做了这个功能:

''----------------- ---------
公共共享函数Read_bin(ByVal ruta As String)
Dim cadena As String =""
Dim dato As Array
If File .Exists(ruta)= True然后
dato = My.Computer.FileSystem.ReadAllBytes(ruta)
对于每个数据作为字节在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.




Freddy Coal写道:
Freddy Coal wrote:

Patrice,每4字节一个文件是我的号码。我认为每个
字节都是一个字符,因为1个字符有8位,而ASCII有255个字符;

我需要获取字符,接受它,加入4个字节得到这个号码。

http://msdn2.microsoft.com/en-us/lib...8t(VS.80).aspx
http://msdn2.microsoft.com/en-us/lib...28(VS .80).aspx

Patrice, each 4 bytes in the file are a number for my. I supose that each
byte is a char, because 1 char have 8bit, and the ASCII have 255 characters;
I need obtain the char, take that, join 4 bytes and get the number.

http://msdn2.microsoft.com/en-us/lib...8t(VS.80).aspx
http://msdn2.microsoft.com/en-us/lib...28(VS.80).aspx



您混淆了文本,二进制数据,代码页和
$ b的概念$ b encoding。

当文本存储在文件中时,它被编码为字节,其中每个

字符由一个或多个字节表示。您正在阅读文件

,假设每个字符始终只是一个字节,

可能根本不是真的。


ASCII编码只包含128个字符,你所指的字符表

通常称为扩展ASCII。它不是一个

编码,所以127以上的字符也取决于应用程序使用的代码页

,所以它们可能根本不是显示的表格。


如果你想把数据作为字节来处理,你根本不应该尝试将它转换成字符串。 .NET中的字符串是unicode,所以Char是16

位,而不是8位。


你可能想查看BinaryReader类,如果你希望阅读

二进制数据,它们代表的不仅仅是字节值。


-

G?ran Andersson

_____
http://www.guffa.com


这篇关于读取二进制文件,代码更改字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆