C
C#8mo ago
Pandetthe

Struct with explicit layout

Hi, I am using Wintab and I am trying to make packet struct, but I have some problem. In some situations field x is uint but in others it's a int. Is there any option to determine if it's now uint or int or I need to have two fields - uint and int at the same offset
6 Replies
reflectronic
reflectronic8mo ago
there is no harm in just choosing one the physical representation of int and uint are identical. in fact, some mathematical operations like +/-/* are also identical for int and uint (like, they do the same operations on the bits and return the same result) if you use int in your struct, but you know that the number is unsigned in some particular instance, you can just cast it to uint (uint)struct.Count and it will work correctly the cast just affects the interpretation of the value by functions like ToString() and operators like >>--it doesn't actually change the bits
Pandetthe
Pandetthe8mo ago
Oh, thank you Now I have two fields at the same offset in native struct, one int and one uint but for final app I am casting it to long to not overcomplicate it with every time checking if it's uint or int and then casting Do you think that will be fine or it's a terrible idea?
reflectronic
reflectronic8mo ago
well. you definitely have to check wheher it's signed or unsigned before casting to long it makes a big difference, whether the extra bits on top will be 1 (to preserve the negativeness) or 0
Pandetthe
Pandetthe8mo ago
I know
reflectronic
reflectronic8mo ago
it seems fine to do that and then cast to long and just deal with long
Pandetthe
Pandetthe8mo ago
Thank you, that's all information I need