dcsimg
Results 1 to 5 of 5

Thread: Calculate message length as 2 bytes

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2020
    Posts
    2

    Calculate message length as 2 bytes

    I have a VB6 application that sends a message over IP to a server. The message is a text message in JSON format. The server app requires me to prefix my message with 2 bytes containing the message length. The length info is sent in network byte order (big-endian). I am looking for help in calculating the message length in the required two byte format.
    TIA
    John

  2. #2
    PowerPoster
    Join Date
    Feb 2017
    Posts
    2,208

    Re: Calculate message length as 2 bytes

    Hello, welcome to the forums.

    If the message is in a String (named in the sample below Message), my first attempt would be:

    Code:
        Dim MessageLen As Integer
        Dim b() As Byte
        Dim s As String
        
        MessageLen = LenB(Message)
        ReDim b(1)
        b(0) = HiByte(MessageLen)
        b(1) = LoByte(MessageLen)
        s = b
        Message = s & Message
    Code:
    Private Function LoByte(Number As Integer) As Byte
        LoByte = Number And &HFF
    End Function
    
    Private Function HiByte(Number As Integer) As Byte
        HiByte = (Number And &HFF00&) \ &H100
    End Function
    If the two bytes for the size must be taken into account as part of the message, then add 2.
    Last edited by Eduardo-; May 26th, 2020 at 04:18 PM.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2020
    Posts
    2

    Re: Calculate message length as 2 bytes

    I received a code snip it from the terminal manufacturer:
    buffer = malloc(data_len + 2);
    buffer[0] = data_len / 256;
    buffer[1] = data_len % 256;
    memcpy(&buffer[2], data, data_len);
    data= buffer;
    data_len+=2;
    Can this be achieved in VB6?

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,913

    Re: Calculate message length as 2 bytes

    That should be the same result as Eduardo's code.
    They add the 2 to data_len after it has been added to the buffer, so the length in the buffer doesn't include the length value, just the number of bytes of the data.
    That is what Eduardo's code (as written) does.
    His code also puts the size in BigEndian order (network order), just as the "code snip" does.
    And his code prepends the length to the front of the message, just as the "code snip" does.

    If it isn't working for you, then try Len instead of LenB. The message may be converted to ASCII so will not be LenB size (memory size), but Len size (character count) when sent.

    If you used wireshark, you could verify what the packet looks like, i.e., whether the Unicode is converted to ASCII or not when packed in a socket UDP packet. If it isn't, and the terminal expects ASCII, then you can convert to ASCII manually. It seems like I've done a lot of UDP with VB6 back in the day, and I don't remember having to convert unicode to ASCII manually, so I think it is automatic. But, most of the time, I didn't send strings over UDP, I sent byte arrays, so converted strings to a series of ASCII byte code as that is what the other side expected anyway.
    Last edited by passel; May 26th, 2020 at 03:49 PM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  5. #5
    PowerPoster
    Join Date
    Feb 2017
    Posts
    2,208

    Re: Calculate message length as 2 bytes

    Hello, I edited my post because I think there was an error, I was sending characters (two bytes) instead of bytes.

    And if you need to send ANSI, the last line should be:

    Code:
    Message = s & StrConv(Message, vbFromUnicode)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width