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?
Re: how to put different Varaibles in one ByteArray
Just double check is this a network question?
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.
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
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.
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 :)
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.