简体   繁体   中英

Using vb.net ReadAllBytes

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?

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

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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