我合併了一個string
的字節和那個長度的字節string
Ex。 stackoverflow13
(可讀的stackoverflow
的字節加上長度爲13
的字節的表示)現在這個組合字節將被髮送到另一個程序,該程序使用Encoding.UTF8.GetString(byte, index, count)
我想分開stackoverflow
和13
的索引count
。我怎樣才能做到這一點。這是代碼:如何從一個字節流中分離一個字節
Dim text1() As Byte = Encoding.UTF8.GetBytes("stackoverflow")
Dim len1() As Byte = BitConverter.GetBytes(text1.Length)
Dim byteLen As Integer = text1.Length + len1.Length
Dim bStream(byteLen) As Byte
text1.CopyTo(bStream, 0)
len1.CopyTo(bStream, text1.Length)
' |
' | Byte stream "bStream"
' V
'-----------------------------------------------------------
'--------------------Different program----------------------
'-----------------------------------------------------------
' |
' | Byte stream "bStream"
' |
' | | n = supposed to be len1
' V V from the stream
Debug.WriteLine(Encoding.UTF8.GetString(bStream, 0, n))
我該如何做到這一點在類似的流多時間?防爆。
fileOne,fileOneLength,fileTwo,fileTwoLength...
爲什麼你需要發送字符串長度? – Steve
由於您未發送「stackoverflow13」,因此您的問題具有誤導性。您正在將整數轉換爲4個字節。您的流的最後4個字節將是該長度的整數。通常這在開始時作爲數據包標題發送。 –