Click here to Skip to main content
15,178,202 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
The unsigned char array is given as
unsigned char dmp1[] =

unsigned char = 1 byte, if we interchage it is same in little endian and big endian formats because we can convert endianness of only multibyte values
Example: Consider the multibyte value as 0x34 0x35 0x46 0x45 .If it is in little endian.
When we convert it to big endian it becomes, 0x45 0x46 0x35 0x34.

If it so, the dmp1 value above should be interchange from the last to first to convert it in to big endian style.

Can any one help explain how it is so?

What I have tried:

I have tried interchanging byte values of dmp1 from last to first but didnt get the output what i expected.
Updated 18-Feb-16 18:56pm
Jochen Arndt 10-Feb-16 5:50am
Conversion depends on how the data are used. If they are just bytes, there is no swapping necessary (the endianess affects only the order of bytes of CPU register values and integral types based on that but not the order of elements stored in memory).

But if they are values of a specific width, they must be retrieved as those (e.g. by casting the array pointer to the required type) and changing the endianess of these.
There is no just "little endian" or "big endian". It can be UTF-16LE/BE, UTF-32LE/BE... The conversion will be different. Also, you should understand that you switch between LE and BE, but you never know what is the final result if you don't know what is the input. It's possible to figure out only in some special cases of separate code points, but this is not what should normally be done; it can be based on BOM.

It's not clear what your problem is, because you did not show any code. Why writing in the "What I have tried section" at all, if you are not showing what you have tried?

CPallini 10-Feb-16 5:58am
What kind of data is it? As already pointed out, big endian <-> little endian conversion makes sense only for multi-byte data size.
Albert Holguin 18-Feb-16 23:48pm
Well, you could do it for one byte but it would be a very simple program.. here it is:
CPallini 19-Feb-16 2:33am

1 solution

Ok, so you probably didn't get the answer you expected because you have to do byte-swapping in reference to some symbol size. As Sergey explained above, the symbol size can be 16 bits, 32 bits, or whatever.

32 bits is a common size (your example uses that size)... so let's go with that...

So what you should do is (in pseudo-code):
  buff[symbol_size] = *dmp1+offset;
  //do your swap here for 32 bits (4 bytes) on buff

Does that make sense? have to do your swapping one symbol at a time, not do the whole buffer at once.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900