我合并了一个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个字节将是该长度的整数。通常这在开始时作为数据包标题发送。 –