Solved

Byte Swaping a structure

Posted on 2003-12-02
3
482 Views
Last Modified: 2008-02-01
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            IDMStaleFilterDurationSpare : 13;
      int            IDMStaleFilterDuration : 19;
      int            IDMOldFilterDurationSpare : 13;
      int            IDMOldFilterDuration : 19;
      int            IDMPurgeFilterDurationSpare : 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            IDMFIPRRegNetworkMonitoring : 1;
      short            IDMFIPRRegSystemCoord : 1;
      short            IDMFIPRRegSecurityMsg : 1;
      short            IDMFIPRRegFreetext : 1;
      short            IDMFIPRRegUnitRefQuery : 1;
      short            IDMFIPRRegInfoRequest : 1;
      short            IDMFIPRRegObserverMsnUpdate : 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            IDMFIPRRegSupplyPointStatus : 1;
      short            IDMFIPRRegTaskMgmt : 1;
      short            IDMFIPRSpare : 11;
      char            IDMEBC_UTO[64];

} IDM_OPERATOR_CONFIG_TYPE;
0
Comment
Question by:LBoorn
  • 3
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9861229
>>Is there some inherent function in C++ like the _swab function that performs this

'_swab()' works like

/* SWAB.C illustrates:
 *      _swab
 */

#include <stdlib.h>
#include <stdio.h>

char from[] = "BADCFEHGJILKNMPORQTSVUXWZY";
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 );
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 9861263
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.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 9861291
OK, here we go

#include <winsock.h> // endianity support
#pragma comment ( lib, "ws2_32.lib")

typedef struct {
    short          IDMSA;
    int                     IDMFilter;
    short          IDMShort1;
    char           IDMFSConfigFile[6];
    short          IDMShort2;
    int            IDMInt;
    char           IDMTIPassword[8];
    short          IDMShort3;
    char          IDMEBC_UTO[64];

} IDM_OPERATOR_CONFIG_TYPE_HELPER;

IDM_OPERATOR_CONFIG_TYPE idm; // original struct
IDM_OPERATOR_CONFIG_TYPE_HELPER* p = &idm; // helper needed fow swapping

p->IDMSA= htons ( p->IDMSA);
p->IDMFilter = htonl ( p->IDMFilter);
p->IDMShort1 = htons ( p->IDMShort1);
p->IDMShort2 = htons ( p->IDMShort2);
p->IDMInt = htonl ( p->IDMInt);
p->IDMShort3 = htons ( p->IDMShort2);

This will avoid the byte swapping for the character members, which certainly is somethig you wouldn't want to happen :o)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Building cUrl in Windows v7.43.0 4 39
how to convert c++ code to Android App 3 132
How do I save this device context as a .bmp or a picture of some kind? 2 71
Outlook 13 84
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question