Results 1 to 7 of 7

Thread: how to put different Varaibles in one ByteArray "Resolved"

  1. #1

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Resolved how to put different Varaibles in one ByteArray "Resolved"

    Hi,
    I need to put different Varaibles into one ByteArray (using CopyMemory). Ican handle it with one Variable, even if it ios a UDT-array. But I need to put in different things, and furthermore they need to be in one message (at least I think so).
    Is there an easy way to put some variables (Singles,Integers, Boolean) into one ByteArray in order to send them in one message?
    Last edited by opus; Jun 27th, 2005 at 12:58 PM.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  2. #2
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: how to put different Varaibles in one ByteArray

    Just double check is this a network question?

  3. #3

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: how to put different Varaibles in one ByteArray

    Come to think about it, really not. I'm sorry, but since it is the way to make my data "sendable" over the network, it "sounded" network for me.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177

    Re: how to put different Varaibles in one ByteArray

    If I understand you, you want to put more than 1 variable into the byte array, correct? The easy way is to put the variables into a UDT and copy that to the byte array. The hard way is to copy each variable to the appropiate offset into the array.

    For example, let's say you want to copy an Integer, a Long, a Boolean, a Byte and another Long, in that order. The first thing you should keep in mind is the byte alignment needed for each variable type. If the variables are going to be removed from the byte array the same way they were inserted (i.e the CopyMemory API) then alignment won't be a problem.

    Anyway, I would do something like the following to fill the byte array.
    Code:
    Const sizeofByte    As Integer = 1
    Const sizeofBoolean As Integer = 2
    Const sizeofInteger As Integer = 2
    Const sizeofLong    As Integer = 4
    
    Dim offset          As Integer
    Dim Int1            As Integer
    Dim Bool1           As Boolean
    Dim Long1           As Long
    Dim Long2           As Long
    Dim Byte1           As Byte
    Dim bArry(20)       As Byte
    
    offset = 0
    CopyMemory bArry(offset), Int1, sizeofInteger
    offset = offset + sizeofInteger
    
    CopyMemory bArry(offset), Long1, sizeofLong
    offset = offset + sizeofLong
    
    CopyMemory bArry(offset), Bool1, sizeofBoolean
    offset = offset + sizeofBoolean
    
    // copymemory not needed here
    bArry(offset) = Byte1           
    offset = offset + sizeofByte
    
    CopyMemory bArry(offset), Long2, sizeofLong
    If you do have to take the proper alignment into consideration, then take this approach:
    Code:
    Const sizeofByte    As Integer = 1
    Const sizeofBoolean As Integer = 2
    Const sizeofInteger As Integer = 2
    Const sizeofLong    As Integer = 4
    
    dim offset          as Integer
    Dim padding         As Integer
    Dim Int1            As Integer
    Dim Bool1           As Boolean
    Dim Long1           As Long
    Dim Long2           As Long
    Dim Byte1           As Byte
    Dim bArry(20)       As Byte
    
    offset = 0
    CopyMemory bArry(offset), Int1, sizeofInteger
    offset = offset + sizeofInteger
    padding = offset Mod sizeofLong
    offset = offset + padding
    
    CopyMemory bArry(offset), Long1, sizeofLong
    offset = offset + sizeofLong
    padding = offset Mod sizeofBoolean
    offset = offset + padding
    
    CopyMemory bArry(offset), Bool1, sizeofBoolean
    offset = offset + sizeofBoolean
    
    // byte alignment and copymemory not needed here
    bArry(offset) = Byte1            
    offset = offset + sizeofByte
    padding = offset Mod sizeofLong
    offset = offset + padding
    
    Copymemory bArry(offset), Long2, sizeofLong

  5. #5

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: how to put different Varaibles in one ByteArray

    Thanks CCODER, it got it now. I have to take the "hard" way, but that's no problem.

    Thanks for the help.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  6. #6
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: how to put different Varaibles in one ByteArray

    Quote Originally Posted by opus
    Come to think about it, really not. I'm sorry, but since it is the way to make my data "sendable" over the network, it "sounded" network for me.
    Oo, osrry i proberly sounded a bit snappy in that message, I just wanted to make sure you got the quickest reply, since its solved theres no problem now

  7. #7
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177

    Re: how to put different Varaibles in one ByteArray

    Quote Originally Posted by opus
    Thanks CCODER, it got it now. I have to take the "hard" way, but that's no problem.

    Thanks for the help.
    Glad I could help. If you ever have to take byte alignment into consideration, do use a UDT - it (actually the compiler) will take care of the byte alignment for you.

Posting Permissions

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



Click Here to Expand Forum to Full Width