Hi all,
I need to convert a double from little-endian mode to big-endian mode but I don't know how? Can anybody help me?
Many thanks, Mehdi
from little-endian mode to big-endian
Page 1 of 110 Replies - 5922 Views - Last Post: 30 June 2010 - 10:05 AM
Replies To: from little-endian mode to big-endian
#2
Re: from little-endian mode to big-endian
Posted 29 June 2010 - 04:04 AM
Well conversion from big to little endian (and vice versa) is just a matter of swapping the order of the bytes in memory.
The problem with a double (or a float) is that there is no platform independent way of doing this.
However assuming that you are happy with a slightly dirty platform dependent method get a pointer to your double or the buffer containing its data and convert that pointer to a pointer to char (this is the platform dependent dirty step). Then you can use the char pointer to swap the bytes in memory.
However I am a little concerned about why you want to do this, however since you have not said what the purpose of the code is though I can't really tell if what you are attempting is logical.
The problem with a double (or a float) is that there is no platform independent way of doing this.
However assuming that you are happy with a slightly dirty platform dependent method get a pointer to your double or the buffer containing its data and convert that pointer to a pointer to char (this is the platform dependent dirty step). Then you can use the char pointer to swap the bytes in memory.
However I am a little concerned about why you want to do this, however since you have not said what the purpose of the code is though I can't really tell if what you are attempting is logical.
#3
Re: from little-endian mode to big-endian
Posted 29 June 2010 - 04:18 AM
Thanks for the comments. But how can I swap the bytes in memory and in which direction? can you please help? I have a double (data) reading from a binary file using this command (part of the code):
is.read(reinterpret_cast<char *>(&data), sizeof(double));
cout << "Read : " << data << endl;
Now how can I convert it to big-endian.
Thanks
is.read(reinterpret_cast<char *>(&data), sizeof(double));
cout << "Read : " << data << endl;
Now how can I convert it to big-endian.
Thanks
#4
Re: from little-endian mode to big-endian
Posted 29 June 2010 - 05:44 AM
#5
Re: from little-endian mode to big-endian
Posted 29 June 2010 - 06:04 AM
Transferring files from one system to another can bring up issues like this.
I'm not sure that swapping bytes will work for doubles , use of bitfields and unions, maybe. I haven't tested this for doubles, though.
You may need to work out the format.
ieee754.h
And use unions and bitfields
Unions
Bitfields
Readable and Maintainable Bitfields in C
ldexp()
I'm not sure that swapping bytes will work for doubles , use of bitfields and unions, maybe. I haven't tested this for doubles, though.
You may need to work out the format.
ieee754.h
And use unions and bitfields
Unions
Bitfields
Readable and Maintainable Bitfields in C
ldexp()
#6
Re: from little-endian mode to big-endian
Posted 29 June 2010 - 12:25 PM
Bitfields are useless for this task, and one of those links is flat-out wrong.
> Bitfields in C always start at bit 0.
It is implementation-defined where the compiler chooses to put the first bits of the first bit-field.
At least the first response puts them right on a number of points.
> Bitfields in C always start at bit 0.
It is implementation-defined where the compiler chooses to put the first bits of the first bit-field.
At least the first response puts them right on a number of points.
#7
Re: from little-endian mode to big-endian
Posted 29 June 2010 - 04:24 PM
Salem_c, on 29 June 2010 - 08:25 PM, said:
Bitfields are useless for this task, and one of those links is flat-out wrong.
> Bitfields in C always start at bit 0.
It is implementation-defined where the compiler chooses to put the first bits of the first bit-field.
At least the first response puts them right on a number of points.
> Bitfields in C always start at bit 0.
It is implementation-defined where the compiler chooses to put the first bits of the first bit-field.
At least the first response puts them right on a number of points.
Don't know what you are talking about, seems fairly simple to me.
Quote
At least the first response puts them right on a number of points.
Could use char pointer and bitwise operators, swapping bytes not so sure that would work.
Quote
However I am a little concerned about why you want to do this, however since you have not said what the purpose of the code is though I can't really tell if what you are attempting is logical.
I've worked on transferring data between different systems, so understand why someone may want to do this.
If I have some spare time I may look at it.
#8
Re: from little-endian mode to big-endian
Posted 29 June 2010 - 05:08 PM
#define, on 29 June 2010 - 10:24 PM, said:
Salem_c, on 29 June 2010 - 08:25 PM, said:
Bitfields are useless for this task, and one of those links is flat-out wrong.
> Bitfields in C always start at bit 0.
It is implementation-defined where the compiler chooses to put the first bits of the first bit-field.
At least the first response puts them right on a number of points.
> Bitfields in C always start at bit 0.
It is implementation-defined where the compiler chooses to put the first bits of the first bit-field.
At least the first response puts them right on a number of points.
Don't know what you are talking about, seems fairly simple to me.
#define, on 29 June 2010 - 10:24 PM, said:
Quote
At least the first response puts them right on a number of points.
Could use char pointer and bitwise operators, swapping bytes not so sure that would work.
#define, on 29 June 2010 - 10:24 PM, said:
Quote
However I am a little concerned about why you want to do this, however since you have not said what the purpose of the code is though I can't really tell if what you are attempting is logical.
I've worked on transferring data between different systems, so understand why someone may want to do this.
This post has been edited by Banfa: 29 June 2010 - 05:10 PM
#9
Re: from little-endian mode to big-endian
Posted 29 June 2010 - 05:46 PM
Quote
Think about it, we are talking about swapping byte order, bit fields deal with bits (the clue is in the name
) not bytes and are therefore not suited.
Quote
(the clue is in the name
)
Lol, I know that it would work for integral types, but I wasn't certain about floats. On further reading/thinking about it, swapping bytes for floats should work. You may say that bit fields are not suited, but it was mentioned that they were useless, which I was really commenting/upset about.
Quote
Quote
I've worked on transferring data between different systems, so understand why someone may want to do this.
I got it into my head that the OP was reading data from a file ... its been a tough day, but good as well.
Quote
Many platforms do use IEEE 754 for floating point representation but it is not mandated. Floating point formats are even less likely than ints to be portable between platforms ...
Yes this was one of the things I was concerned about.
Thanks for your thoughtful post, it has cheered me up, I'm glad at least one of us is on the ball today.
This post has been edited by #define: 29 June 2010 - 05:54 PM
#10
Re: from little-endian mode to big-endian
Posted 29 June 2010 - 08:03 PM
Quote
You may say that bit fields are not suited, but it was mentioned that they were useless, which I was really commenting/upset about.
Just think about this. You need a way to deal with endianness. You propose using bitfields, which depend on the implementation, and quite likely, the endianness. Your solution to dealing with endianness may fail because of dependency on endianness.
I think that characterizes an utterly useless solution.
#11
Re: from little-endian mode to big-endian
Posted 30 June 2010 - 10:05 AM
First, a bit of history.
http://plan9.bell-la.../dmr/chist.html
C was born 40 years ago on machines with a lot less than 64K of memory. Note that is K, not M or G.
"K" to "young'uns" today is a rounding error!
Bitfields (and unions) were an excellent way of stuffing many small values into a compact space. For the first 10 years, the lack of a detailed language spec, a single reference compiler and not a lot of concern over portability (that came later) wasn't a problem. Bitfields were portable, because there was only one implementation.
The 80's arrived, and a number of different C compilers sprang up to cater to the emerging PC market. These obviously did a lot of things differently in the details. Eventually, ANSI got a grip on the problem and produced a standard towards the end of the decade (ANSI-C89 and ISO-C90).
Since one of ANSI's remits was to "not break existing usage", and that a lot of existing usage of bit-fields did things differently. Also, the committee did not want to introduce anything which might hinder efficient implementation, with statements like "bitfields shall be packed into 32-bit words, starting with the most significant bit".
The result is that bitfields are almost entirely "implementation specific".
Essentially, all bets are off if you try to expose a bit-field to the outside world.
How many tutorials do you see on the web, where say a BMP header maps directly to a C struct? It 'worked' at the time, because the only compilers that had to deal with it all did it in the same way. Unfortunately, "happened to work 20 years ago" does not equate to a reliable coding technique today.
There might be a few less popular compilers nowadays, but the number of architectures has mushroomed. The number of permutations of endian, padding and alignment options is bewildering. Some machines (eg MIPS) can even flip endian mode at run-time.
The grim reality is, if you want to serialise your data to/from some external source, then you have to basically pick it apart yourself a byte at a time.
The days of simply being able to memcpy() a block of data onto a struct of some sort, and have it all done for you are gone.
http://plan9.bell-la.../dmr/chist.html
C was born 40 years ago on machines with a lot less than 64K of memory. Note that is K, not M or G.
"K" to "young'uns" today is a rounding error!
Bitfields (and unions) were an excellent way of stuffing many small values into a compact space. For the first 10 years, the lack of a detailed language spec, a single reference compiler and not a lot of concern over portability (that came later) wasn't a problem. Bitfields were portable, because there was only one implementation.
The 80's arrived, and a number of different C compilers sprang up to cater to the emerging PC market. These obviously did a lot of things differently in the details. Eventually, ANSI got a grip on the problem and produced a standard towards the end of the decade (ANSI-C89 and ISO-C90).
Since one of ANSI's remits was to "not break existing usage", and that a lot of existing usage of bit-fields did things differently. Also, the committee did not want to introduce anything which might hinder efficient implementation, with statements like "bitfields shall be packed into 32-bit words, starting with the most significant bit".
The result is that bitfields are almost entirely "implementation specific".
Essentially, all bets are off if you try to expose a bit-field to the outside world.
How many tutorials do you see on the web, where say a BMP header maps directly to a C struct? It 'worked' at the time, because the only compilers that had to deal with it all did it in the same way. Unfortunately, "happened to work 20 years ago" does not equate to a reliable coding technique today.
There might be a few less popular compilers nowadays, but the number of architectures has mushroomed. The number of permutations of endian, padding and alignment options is bewildering. Some machines (eg MIPS) can even flip endian mode at run-time.
The grim reality is, if you want to serialise your data to/from some external source, then you have to basically pick it apart yourself a byte at a time.
The days of simply being able to memcpy() a block of data onto a struct of some sort, and have it all done for you are gone.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote






|