LBoorn
asked on
Byte Swaping a structure
I would like to set data elements to a struction and then byte swap the entire structure.
The part that makes it difficult is the struction contains elements of diffrent sizes. Below is an example of a data structure that uses shorts, ints, and chars all in the same structure.
I do not have to perform any word swapping just byte swapping.
Is there some inherent function in C++ like the _swab function that performs this. If not does someone have an example of how to do this. Once the structure is swapped it will be written to a file.
#pragma pack(1)//eliminate padding in structs
typedef struct {
short IDMSAReportingMode : 2;
short IDMSAReportingSpare : 1;
short IDMSATimeFilter : 13;
short IDMSAMotionSpare : 4;
short IDMSAMotionFilter : 12;
int IDMStaleFilterDurationSpar e : 13;
int IDMStaleFilterDuration : 19;
int IDMOldFilterDurationSpare : 13;
int IDMOldFilterDuration : 19;
int IDMPurgeFilterDurationSpar e : 13;
int IDMPurgeFilterDuration : 19;
short IDMAutoResponseModeSpare : 6;
short IDMAutoResponseMode : 2;
short IDMChannelTransFlagSpare : 6;
short IDMChannelTransFlag : 2;
char IDMFSConfigFile[6];
short IDMTIInterfaceSpare : 6;
short IDMTIInterfaceNum : 2;
short IDMTICMSAPortNumSpare : 6;
short IDMTICMSAPortNum : 2;
short IDMFSInterfaceNumSpare : 6;
short IDMFSInterfaceNum : 2;
short IDMFSCMSAPortSpare : 6;
short IDMFSCMSAPort : 2;
int IDMEBC_OWN_URN_Spare : 9;
int IDMEBC_OWN_URN : 23;
int IDMMulticast_URN_Spare : 9;
int IDMMulticast_URN : 23;
char IDMTIPassword[8];
short IDMFIPRRegNetworkMonitorin g : 1;
short IDMFIPRRegSystemCoord : 1;
short IDMFIPRRegSecurityMsg : 1;
short IDMFIPRRegFreetext : 1;
short IDMFIPRRegUnitRefQuery : 1;
short IDMFIPRRegInfoRequest : 1;
short IDMFIPRRegObserverMsnUpdat e : 1;
short IDMFIPRRegMsgToObserver : 1;
short IDMFIPRRegAirborneFireMsn : 1;
short IDMFIPRRegBasicWeatherRprt : 1;
short IDMFIPRRegMaydayMsg : 1;
short IDMFIPRRegSpot_SaluteRprt : 1;
short IDMFIPRRegObstacleRprt : 1;
short IDMFIPRRegPosRprt : 1;
short IDMFIPRRegNBC3Rprt : 1;
short IDMFIPRRegREDCONRprt : 1;
short IDMFIPRRegThreatWarning : 1;
short IDMFIPRRegSitrep : 1;
short IDMFIPRRegMOPP : 1;
short IDMFIPRRegSupplyPointStatu s : 1;
short IDMFIPRRegTaskMgmt : 1;
short IDMFIPRSpare : 11;
char IDMEBC_UTO[64];
} IDM_OPERATOR_CONFIG_TYPE;
The part that makes it difficult is the struction contains elements of diffrent sizes. Below is an example of a data structure that uses shorts, ints, and chars all in the same structure.
I do not have to perform any word swapping just byte swapping.
Is there some inherent function in C++ like the _swab function that performs this. If not does someone have an example of how to do this. Once the structure is swapped it will be written to a file.
#pragma pack(1)//eliminate padding in structs
typedef struct {
short IDMSAReportingMode : 2;
short IDMSAReportingSpare : 1;
short IDMSATimeFilter : 13;
short IDMSAMotionSpare : 4;
short IDMSAMotionFilter : 12;
int IDMStaleFilterDurationSpar
int IDMStaleFilterDuration : 19;
int IDMOldFilterDurationSpare : 13;
int IDMOldFilterDuration : 19;
int IDMPurgeFilterDurationSpar
int IDMPurgeFilterDuration : 19;
short IDMAutoResponseModeSpare : 6;
short IDMAutoResponseMode : 2;
short IDMChannelTransFlagSpare : 6;
short IDMChannelTransFlag : 2;
char IDMFSConfigFile[6];
short IDMTIInterfaceSpare : 6;
short IDMTIInterfaceNum : 2;
short IDMTICMSAPortNumSpare : 6;
short IDMTICMSAPortNum : 2;
short IDMFSInterfaceNumSpare : 6;
short IDMFSInterfaceNum : 2;
short IDMFSCMSAPortSpare : 6;
short IDMFSCMSAPort : 2;
int IDMEBC_OWN_URN_Spare : 9;
int IDMEBC_OWN_URN : 23;
int IDMMulticast_URN_Spare : 9;
int IDMMulticast_URN : 23;
char IDMTIPassword[8];
short IDMFIPRRegNetworkMonitorin
short IDMFIPRRegSystemCoord : 1;
short IDMFIPRRegSecurityMsg : 1;
short IDMFIPRRegFreetext : 1;
short IDMFIPRRegUnitRefQuery : 1;
short IDMFIPRRegInfoRequest : 1;
short IDMFIPRRegObserverMsnUpdat
short IDMFIPRRegMsgToObserver : 1;
short IDMFIPRRegAirborneFireMsn : 1;
short IDMFIPRRegBasicWeatherRprt
short IDMFIPRRegMaydayMsg : 1;
short IDMFIPRRegSpot_SaluteRprt : 1;
short IDMFIPRRegObstacleRprt : 1;
short IDMFIPRRegPosRprt : 1;
short IDMFIPRRegNBC3Rprt : 1;
short IDMFIPRRegREDCONRprt : 1;
short IDMFIPRRegThreatWarning : 1;
short IDMFIPRRegSitrep : 1;
short IDMFIPRRegMOPP : 1;
short IDMFIPRRegSupplyPointStatu
short IDMFIPRRegTaskMgmt : 1;
short IDMFIPRSpare : 11;
char IDMEBC_UTO[64];
} IDM_OPERATOR_CONFIG_TYPE;
BTW, as I can see, you don *NOT* want to use '_swab()' at all - if your goal is to put the above in non-intel byte order, just swapping the byte is not enough, you need to use the appropriate functions on the bitfield members.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
'_swab()' works like
/* SWAB.C illustrates:
* _swab
*/
#include <stdlib.h>
#include <stdio.h>
char from[] = "BADCFEHGJILKNMPORQTSVUXWZ
char to[] = ".........................
void main()
{
printf( "Before:\t%s\n\t%s\n\n", from, to );
_swab( from, to, sizeof( from ) );
printf( "After:\t%s\n\t%s\n\n", from, to );
}