Dividing the bits in a 16 bit short value
-
I have a structure: typedef struct{ unsigned short Id : 8; unsigned short Name: 8; unsigned short Length: 11; unsigned short Value: 5; } MyStruct; I also have an array of bytes (unsigned char): I want to stick that structure into the first four bytes in the array. Array[0] = MyStruct->Id; Array[1] = MyStruct->Name; ...then what? How can I divide the last two values and put them into the [2] and [3] position in the byte array?
-
I have a structure: typedef struct{ unsigned short Id : 8; unsigned short Name: 8; unsigned short Length: 11; unsigned short Value: 5; } MyStruct; I also have an array of bytes (unsigned char): I want to stick that structure into the first four bytes in the array. Array[0] = MyStruct->Id; Array[1] = MyStruct->Name; ...then what? How can I divide the last two values and put them into the [2] and [3] position in the byte array?
-
I have a structure: typedef struct{ unsigned short Id : 8; unsigned short Name: 8; unsigned short Length: 11; unsigned short Value: 5; } MyStruct; I also have an array of bytes (unsigned char): I want to stick that structure into the first four bytes in the array. Array[0] = MyStruct->Id; Array[1] = MyStruct->Name; ...then what? How can I divide the last two values and put them into the [2] and [3] position in the byte array?
Please note sizes are identical. Methods (1) quick & dirty: copy memory
MyStruct m;
unsigned char a[4];
memcpy(a, &m, sizeof(a));(2) perhaps more elegant: use union
typedef union
{
MyStruct m;
unsigned char a[4];
}MyUnion;MyUnion mu;
unsigned char array[4];
// initialize mu.m.Id, mu.m.Name, mu.m.Length, mu.m.Value
for (i=0; i<4; i++)
{
array[i]=mu.a[i];
}If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Please note sizes are identical. Methods (1) quick & dirty: copy memory
MyStruct m;
unsigned char a[4];
memcpy(a, &m, sizeof(a));(2) perhaps more elegant: use union
typedef union
{
MyStruct m;
unsigned char a[4];
}MyUnion;MyUnion mu;
unsigned char array[4];
// initialize mu.m.Id, mu.m.Name, mu.m.Length, mu.m.Value
for (i=0; i<4; i++)
{
array[i]=mu.a[i];
}If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeIt looks like the memcpy might work. I didn't really want to go the route of using a Union. I was actually looking for a method to do this using bit shifting. Something like... unsigned char NewChar = MyStruct->Length << 8; which would put the 8 leftmost bits in Length into the NewChar, which you could put into Array[2]; but then, how do I get the other three bits from Length and merge them with the 5 bits from MyStruct->Value? I kind of know what I want to do it, but I don't know exactly how, what commands to use or what I'm doing.
-
It looks like the memcpy might work. I didn't really want to go the route of using a Union. I was actually looking for a method to do this using bit shifting. Something like... unsigned char NewChar = MyStruct->Length << 8; which would put the 8 leftmost bits in Length into the NewChar, which you could put into Array[2]; but then, how do I get the other three bits from Length and merge them with the 5 bits from MyStruct->Value? I kind of know what I want to do it, but I don't know exactly how, what commands to use or what I'm doing.
Just for the academy (since memcpy is more concise and probably faster).
MyStruct m;
unsigned char a[4];
a[2] = (unsigned char) m.Length; // this cuts off the 3 MSB bits.
a[3] = (m.Length >> 8) | (m.Value << 3);:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Just for the academy (since memcpy is more concise and probably faster).
MyStruct m;
unsigned char a[4];
a[2] = (unsigned char) m.Length; // this cuts off the 3 MSB bits.
a[3] = (m.Length >> 8) | (m.Value << 3);:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeCool. That's what I was looking for originally. But the memcpy works. THANKS!