简体   繁体   English

使用vb.net ReadAllBytes

[英]Using vb.net ReadAllBytes

I used vb .NET function ReadAllBytes to read a file and send it over a socket. 我使用vb .NET函数ReadAllBytes读取文件并通过套接字发送。 When received, I used WriteAllBytes. 收到后,我使用了WriteAllBytes。 The problem is they are not same size! 问题是它们的大小不一样! The original is 16kb, but the received data is 24kb. 原始的是16kb,但是接收到的数据是24kb。 My code is below. 我的代码如下。 What am I doing wrong? 我究竟做错了什么?

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)

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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM