Android Question Socket Send Bytes Array


Licensed User
Hello, I have the following code in

 Public Shared Sub Send(ByVal msg As String)
            Using MS As New MemoryStream
                Dim B As Byte() =StringToBytes(msg)
                Dim L As Byte() = SB(B.Length & CChar(vbNullChar))
                MS.Write(L, 0, L.Length)
                MS.Write(B, 0, B.Length)
                S.Poll(-1, SelectMode.SelectWrite)
                S.Send(MS.ToArray, 0, MS.Length, SocketFlags.None)
            End Using
        Catch ex As Exception
        End Try
    End Sub
It works perfectly

I need to move to b4a, tried this but it does not work

    Dim sb As StringBuilder
    Dim cmd_String As String
    Dim buffer() As Byte
    sb.append(Asc("4")) .append(Asc("abcd"))
    cmd_String = sb.ToString

    astream.Write2(buffer, 0, buffer.Length)
How to represent the? CChar(vbNullChar)

The logic is to send the lenght size of the bytes, then "CChar(vbNullChar)" and send the all bytes in an array.

How to do this with b4a?


Well-Known Member
Licensed User
All CChar does is return the first character of a given expression. Thats it. So CChar prints the character representation of vbNullChar.

Youll probably need byteconverter library and use the ASCII code for Null. However, vbNullChar is just a 0. or ASCII 0x00, so equivalently, your returning the print character of the value 0. in the case of B4X, that would be Chr(0)

when designing/using protocols like this, you really need to understand on a binary level what is going on, which will help you at porting code between languages.
Last edited:


Licensed User
tried this, but it did not work, am I doing it right?

I need to send:
Lenght bytes null character content bytes

Dim xLenghtString As String = "4"
            Dim BC As ByteConverter
            Dim PicWrd() As Byte
            PicWrd = BC.CharsToBytes(Array As Char(Chr(0), Chr(0), Chr(0)))
            astream.Write2(PicWrd, 0 , PicWrd.Length)
            Dim xChar As Char
            For i = 0 To xTesteString.Length - 1
                xChar = xChar & Asc(xTesteString.CharAt(i))


Well-Known Member
Licensed User
Again, you need to study the protocol or know the protocol.

If you log the raw binary output as a hex-string from your VB.Net program, and then compare it against the binary hex-string output from the B4A program, then it will become apparent where the problem is.

I cannot help you further, and I dont think anyone else can either without knowing the full scope of the project, the expected input, and the expected output.



Licensed User
I just need to send an array of bytes, I tried OutputStream

But I did not succeed.

Last time I left my project on this forum and nobody helped.
Leave novalmente for what?

    Dim MemoryStream As OutputStream
            MemoryStream.WriteBytes(xtesteaaa, 0 , xtesteaaa.Length)
            'MemoryStream.WriteBytes(xByte2, 0 , xByte2.Length)
            MemoryStream.WriteBytes(xByte3, 0 , xByte3.Length)