|
-
Jun 26th, 2005, 01:09 PM
#1
-
Jun 26th, 2005, 05:43 PM
#2
Re: how to put different Varaibles in one ByteArray
Just double check is this a network question?
-
Jun 27th, 2005, 12:06 AM
#3
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!
-
Jun 27th, 2005, 09:17 AM
#4
Frenzied Member
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
-
Jun 27th, 2005, 12:58 PM
#5
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!
-
Jun 27th, 2005, 01:01 PM
#6
Re: how to put different Varaibles in one ByteArray
 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
-
Jun 27th, 2005, 04:39 PM
#7
Frenzied Member
Re: how to put different Varaibles in one ByteArray
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|