473,508 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

experimenting with unions - byteswap approach..


Consider the source snippet:

union to_uchar
{
int i;
unsigned int ui;
long l;
unsigned long ul;
short s;
unsigned short us;
double d;
unsigned char uc[8];

void byteSwap(int siz)
{
int idx = 0;
int jdx = siz-1;
while (idx < jdx)
{
std::swap(uc[idx], uc[jdx]);
idx++, jdx--;
}
}
to_uchar(int ii) : i(ii) { swap(i); }
to_uchar(double dd) : d(dd) { swap(d); }
unsigned char* getBuf() { return uc; }

static void swap(int& i)
{
to_uchar x = i; //[1]
x.byteSwap(sizeof(i));
i = x.i;
}
static void swap(double& d)
{
to_uchar x = d; //[2]
x.byteSwap(sizeof(d));
d = x.d;
}

};
int main()
{
double d = 12345.678;
to_uchar::swap(d);
int i = 12345;
to_uchar::swap(i);

to_uchar iuc(12345);
unsigned char* bufi = iuc.getBuf();
to_uchar duc(12345.678);
unsigned char* bufd = duc.getBuf();
}

the lines indicated by [1] and [2] above results in exceptions but this
is one of those cases where I dont fully understand why or what teh
solution is.

Thanks in advance

Sep 1 '05 #1
7 3487
<ma******@pegasus.cc.ucf.edu> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...

Consider the source snippet:

union to_uchar
{
int i;
unsigned int ui;
long l;
unsigned long ul;
short s;
unsigned short us;
double d;
unsigned char uc[8];

void byteSwap(int siz)
{
int idx = 0;
int jdx = siz-1;
while (idx < jdx)
{
std::swap(uc[idx], uc[jdx]);
idx++, jdx--;
}
}
to_uchar(int ii) : i(ii) { swap(i); }
to_uchar(double dd) : d(dd) { swap(d); }
unsigned char* getBuf() { return uc; }

static void swap(int& i)
{
to_uchar x = i; //[1]
x.byteSwap(sizeof(i));
i = x.i;
}
static void swap(double& d)
{
to_uchar x = d; //[2]
x.byteSwap(sizeof(d));
d = x.d;
}

};
int main()
{
double d = 12345.678;
to_uchar::swap(d);
int i = 12345;
to_uchar::swap(i);

to_uchar iuc(12345);
unsigned char* bufi = iuc.getBuf();
to_uchar duc(12345.678);
unsigned char* bufd = duc.getBuf();
}

the lines indicated by [1] and [2] above results in exceptions but this
is one of those cases where I dont fully understand why or what teh
solution is.


I'm not sure what you're trying to do here, but your constructors are a
little screwy, and it has nothing to do with unions.

This constructor...

to_uchar(int ii) : i(ii) { swap(i); }

calls void to_uchar::swap(int& i)...

to_uchar x = i; //[1]

which constructs a to_uchar from an int (invokes the to_uchar(int ii)
constructor)...

to_uchar(int ii) : i(ii) { swap(i); }

which calls void to_uchar::swap(int& i)...

to_uchar x = i; //[1]

which constructs a to_uchar from an int (invokes the to_uchar(int ii)
constructor)...

I think you can see where this is going (infinite recursion). It looks like
the other constructor suffers from the same problem.

--
David Hilsee
Sep 1 '05 #2

ma******@pegasus.cc.ucf.edu wrote:

[]
int main()
{
double d = 12345.678;
to_uchar::swap(d);
int i = 12345;
to_uchar::swap(i);

to_uchar iuc(12345);
unsigned char* bufi = iuc.getBuf();
to_uchar duc(12345.678);
unsigned char* bufd = duc.getBuf();
}


Why don't you use std::reverse()?

template<class T>
void byteswap(T* t)
{
std::reverse(reinterpret_cast<char*>(t), reinterpret_cast<char*>(t
+ 1));
}

By the way, you can't byteswap a floating point number (float, double,
....), since not any bit pattern is a valid floating point number binary
representation.

Sep 1 '05 #3

|| By the way, you can't byteswap a floating point number (float,
double,
|| ...), since not any bit pattern is a valid floating point number
binary
|| representation.

Maxim, thanks for the suggestion.... So what do you do with floats
(doubles) when you have endian issues across platforms? IOW the
sender transmits - say 9.2 and the receiver sees some convoluted number.

Sep 1 '05 #4

ma******@pegasus.cc.ucf.edu wrote:
|| By the way, you can't byteswap a floating point number (float,
double,
|| ...), since not any bit pattern is a valid floating point number
binary
|| representation.

Maxim, thanks for the suggestion.... So what do you do with floats
(doubles) when you have endian issues across platforms? IOW the
sender transmits - say 9.2 and the receiver sees some convoluted number.


I use some binary format and a library, such as XDR.

Alternatively, you may extract mantissa and exponent as integers, send
and reassemble the number on the receiving side.

Sep 1 '05 #5
ma******@pegasus.cc.ucf.edu wrote:
Maxim, thanks for the suggestion.... So what do you do with floats
(doubles) when you have endian issues across platforms? IOW the
sender transmits - say 9.2 and the receiver sees some convoluted number.


You could also use text since different processors sometimes use
different floating point formats. See these FAQs:

http://www.parashift.com/c++-faq-lit...alization.html

Cheers! --M

Sep 1 '05 #6
Ironically, you brought this up becasue serilization is the approach
used.
I need to delve more into the text 'approach' so I suspect I'll peruse
the web to get a feel for how this all works (somebody should have
something that shows this).

Being a little more than an intermediate apprentice :) it'll take me a
few to get this right.

Thanks

Sep 1 '05 #7
ma******@pegasus.cc.ucf.edu wrote:
Ironically, you brought this up becasue serilization is the approach
used.
I need to delve more into the text 'approach' so I suspect I'll peruse
the web to get a feel for how this all works (somebody should have
something that shows this).

Being a little more than an intermediate apprentice :) it'll take me a
few to get this right.

Thanks


You might also want to consider using the Boost serialization library:

http://boost.org/libs/serialization/doc/index.html

Cheers! --M

Sep 8 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
5254
by: David | last post by:
Some developers in my group are using UNIONS to define their data types in a C++ program for an embedded system. Are there any pro and cons in doing this when you can define a CLASS to do the same...
6
13361
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
23
2790
by: rohit | last post by:
Hi, In my couple of years of experience, I have never found a single instance where I needed to use unions and bitfields(though I have used structures).I was just imagining where would these find...
6
6660
by: Chuck Bowling | last post by:
I have a struct that i want to emulate a C++ style union: public struct Samp { public byte byteBuf; public int intBuf; public Samp(int sz) {
1
2379
by: qwerty2_reverse_iterator | last post by:
Is this a bug with the ms compiler (V7.1)? (It seems so at least.) I get errors when I don't initialize all the const pointer fields of an anonymous union in a struct. Example: //T2.h...
4
1348
by: qqqmac | last post by:
FILE file; union { long l; char c; } r; long x;
4
1749
by: uralmutlu | last post by:
Hi, I was wandering if I can have classes in unions? I basically have source code in a format very similar to: union example { ClassA variable1; ClassB variable2; };
67
3295
by: bluejack | last post by:
A recent post asking for help with unions reminded me of this component of the C language that I have only used a couple of times, and those almost entirely out of personal whim -- Unions for the...
11
1995
by: pereges | last post by:
Hello, can some one please guide me a little into using unions. I read about unions in K & R but I am finding it difficult to apply to my problem at hand. I want to save up some space by using...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7495
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.