使用 vb.net ReadAllBytes
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27041693/
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
Using vb.net ReadAllBytes
提问by user1642036
I used vb .NET function ReadAllBytes to read a file and send it over a socket. When received, I used WriteAllBytes. The problem is they are not same size! The original is 16kb, but the received data is 24kb. My code is below. What am I doing wrong?
我使用 vb .NET 函数 ReadAllBytes 读取文件并通过套接字发送它。收到后,我使用了 WriteAllBytes。问题是它们的大小不一样!原来是16kb,但是接收到的数据是24kb。我的代码如下。我究竟做错了什么?
Dim bteRead() As Byte
Try
bteRead = IO.File.ReadAllBytes(filepath)
Catch ex As System.IO.IOException
End Try
Return bteRead
then i convert bytes to string and send it , and when received i convert it back from string to bytes and do the WriteAllBytes
然后我将字节转换为字符串并发送它,当收到时我将它从字符串转换回字节并执行 WriteAllBytes
Dim str As String = a(1)
Dim encod As New System.Text.UTF8Encoding
Dim byteData() As Byte = encod.GetBytes(str)
IO.File.WriteAllBytes("c:\lol.db", byteData)
回答by user1642036
Solution for me was to change:
我的解决方案是改变:
Dim encod As New System.Text.UTF8Encoding
Dim byteData() As Byte = encod.GetBytes(str)
To
到
Dim byteData() As Byte = System.Text.Encoding.Default.GetBytes(str)

