Page 1 of 1 |
|
Posted: Mon, 4th Mar 2013 21:14 Post subject: Need an algorithm that changes byte order |
|
 |
I need to convert huge byte sequences from big to little endian - in this case they come (mostly) in groups of 2 bytes that need to be swapped.
e.g.
convert
00 44 FF CE F4 CF FF DC 0B 6D 00 1A 07 F8 00 CA FF D3 F4 D0
to
44 00 CE FF CF F4 DC FF 6D 0B 1A 00 F8 07 CA 00 D3 FF D0 F4
is there anything that can do this, I couldnt find any copy paste converter on the web
|
|
Back to top |
|
 |
LeoNatan
Banned
Posts: 73193
Location: Ramat Gan, Israel 🇮🇱
|
Posted: Mon, 4th Mar 2013 21:46 Post subject: |
|
 |
|
|
Back to top |
|
 |
|
Posted: Mon, 4th Mar 2013 22:42 Post subject: |
|
 |
Fastest way is the bswap CPU instruction.
For Visual C++ you can use an intrinsic method in order to avoid breaking compiler optimizations when using __asm blocks:
myData[i] = _byteswap_ushort(myData[i])
Equivalent intrinsic method exists on most other compilers but you'll have to google the name of it.
|
|
Back to top |
|
 |
LeoNatan
Banned
Posts: 73193
Location: Ramat Gan, Israel 🇮🇱
|
Posted: Mon, 4th Mar 2013 22:46 Post subject: |
|
 |
BearishSun wrote: | Fastest way is the bswap CPU instruction.
For Visual C++ you can use an intrinsic method in order to avoid breaking compiler optimizations when using __asm blocks:
myData[i] = _byteswap_ushort(myData[i])
Equivalent intrinsic method exists on most other compilers but you'll have to google the name of it. |
Hm interesting, http://www.codeproject.com/Articles/29807/Endian-Conversion-in-ARM-and-x86-Assembly
|
|
Back to top |
|
 |
|
Posted: Tue, 5th Mar 2013 07:28 Post subject: |
|
 |
BearishSun wrote: | Fastest way is the bswap CPU instruction.
For Visual C++ you can use an intrinsic method in order to avoid breaking compiler optimizations when using __asm blocks:
myData[i] = _byteswap_ushort(myData[i])
Equivalent intrinsic method exists on most other compilers but you'll have to google the name of it. |
thanks, so I just need to write a program that takes a txt file with the hex values as input, does the operation myData[i] = _byteswap_ushort(myData[i]) on it and it then it outputs the correct byte-swapped sequence?
(yeah I still cant code, maybe this would be a good first exercise to start on)
|
|
Back to top |
|
 |
|
Posted: Tue, 5th Mar 2013 08:03 Post subject: |
|
 |
You need to:
1. Read all the bytes from a file
2. Iterate over all the bytes (or pair of bytes in your case)
3. Each iteration call _byteswap_ushort and store the result
4. (Optionally) Write the swapped bytes back into a file
Step 3. is what I described, and other steps you can easily find on the net.
|
|
Back to top |
|
 |
|
Posted: Tue, 5th Mar 2013 08:19 Post subject: |
|
 |
|
|
Back to top |
|
 |
Page 1 of 1 |
All times are GMT + 1 Hour |