1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

Android Question Socket Send Bytes Array

Discussion in 'Android Questions' started by Amanda Sura, May 17, 2019.

  1. Amanda Sura

    Amanda Sura Member Licensed User

    Hello, I have the following code in vb.net

    Code:
    Public Shared Sub Send(ByVal msg As String)
            
    Try
                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
                isDisconnected()
            
    End Try
        
    End Sub
    It works perfectly

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

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

        buffer=cmd_String.GetBytes(
    "UTF8")
        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?
     
  2. techknight

    techknight 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: May 17, 2019
  3. Amanda Sura

    Amanda Sura Member Licensed User

    Usage:
    sb.append(Asc("4")) 0x00 .append(Asc("abcd"))

    ???
     
  4. techknight

    techknight Well-Known Member Licensed User

    Edit: its actually Chr(0). Asc does the opposite.
     
    Last edited: May 17, 2019
  5. Amanda Sura

    Amanda Sura Member Licensed User

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

    I need to send:
    Lenght bytes null character content bytes

    Code:
    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
                    
    Log(Asc(xTesteString.CharAt(i)))
                    xChar = xChar & 
    Asc(xTesteString.CharAt(i))
                
    Next
     
  6. Amanda Sura

    Amanda Sura Member Licensed User

    Someone ?
     
  7. techknight

    techknight 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.

    Sorry.
     
  8. Amanda Sura

    Amanda Sura Member 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?

    Code:
    Dim MemoryStream As OutputStream
                MemoryStream.InitializeToBytesArray(
    0)
                MemoryStream.WriteBytes(xtesteaaa, 
    0 , xtesteaaa.Length)
                
    'MemoryStream.WriteBytes(xByte2, 0 , xByte2.Length)
                MemoryStream.WriteBytes(xByte3, 0 , xByte3.Length)
     
  9. Erel

    Erel Administrator Staff Member Licensed User

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice