Click here to Skip to main content
15,892,643 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[] =
"\x54\x49\x4C\xA6\x3E\xBA\x03\x37\xE4\xE2\x40\x23\xFC\xD6\x9A\x5A"
"\xEB\x07\xDD\xDC\x01\x83\xA4\xD0\xAC\x9B\x54\xB0\x51\xF2\xB1\x3E"
"\xD9\x49\x09\x75\xEA\xB7\x74\x14\xFF\x59\xC1\xF7\x69\x2E\x9A\x2E"
"\x20\x2B\x38\xFC\x91\x0A\x47\x41\x74\xAD\xC9\x3C\x1F\x67\xC9\x81";


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.
Posted
Updated 18-Feb-16 17:56pm
Comments
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.
Sergey Alexandrovich Kryukov 10-Feb-16 5:50am    
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?

—SA
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):
C++
offset=0;
while(offset<dmp1_size){>
  buff[symbol_size] = *dmp1+offset;
  //do your swap here for 32 bits (4 bytes) on buff
  offset+=symbol_size;
}


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

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