conquistador conquistador - 1 month ago 6
Vb.net Question

How to separate a byte from a byte stream

I merged the bytes of a

string
and the byte of the length of that
string

Ex.
stackoverflow13
(readable presentation of the byte of
stackoverflow
plus the bytes of the length
13
) now this combined bytes will be sent to another program that uses
Encoding.UTF8.GetString(byte, index, count)
I want to separate the index of
stackoverflow
and
13
for the
count
. How can I do that. This is the code:

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))


How can I do this multiple timea on the similar stream? Ex.

fileOne,fileOneLength,fileTwo,fileTwoLength...

Answer

Can you use a delimiter to tell the other program where to start looking for the length data such as "-" so it would be "stackloverflow-13".

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    ' Parsing using a  delimeter 
    Dim text1() As Byte = Encoding.UTF8.GetBytes("stackoverflow" & "-") ' concatenate a delimter in the string
    '   Convert to string, otherwise the program sends the byte value of 13 plus 3 padded zeros so you end up transmitting a Carriage return and 3 null characters
    Dim len1() As Byte = Encoding.UTF8.GetBytes(text1.Length - 1.ToString) ' Subtract the delimteter character value from the total
    Dim byteLen As Integer = text1.Length + len1.Length
    Dim bStream(byteLen) As Byte
    text1.CopyTo(bStream, 0)
    len1.CopyTo(bStream, text1.Length)


    ' This goes in the other program 
    Dim Str() As String = Encoding.UTF8.GetString(bStream, 0, bStream.Length).Split("-")

    Dim Text As String = Str(0)
    Dim Index As Integer = CInt(Str(1))


End Sub