Page 1 of 1

Little Endian to Big Endian

Posted: Sat Sep 21, 2013 3:32 am
by Rocky5
Hi, was wondering if its possible to convert a hole file to Big Endian, I have been trying for hours, even tried C++ (made my head hurt) but to no avail.

I tried hacking up the DDS endian swapper but that dint work, it seemed to be in a loop.

Basically I have a 1.00mb file the first 4 bytes are big endian the next 1048612 bytes are little endian, these are 32 bit unsigned long.

I can do it manually but I was making a script to do it & failing.

I'm not even sure its possible to do.

any help would be very much appreciated.

Re: Little Endian to Big Endian

Posted: Sat Sep 21, 2013 1:20 pm
by aluigi
you can try the -E option of quickbms that was added just for this job.

use the following script:

Code: Select all

get SIZE asize
math OFFSET = 4
math SIZE -= OFFSET
goto OFFSET
math SIZE /= 4
for i = 0 < SIZE
    get TMP long
next i
and then launch it with the following command:

Code: Select all

quickbms -w -E script.bms your_file.dds
the changes will be applied directly to the input file

Re: Little Endian to Big Endian

Posted: Sat Sep 21, 2013 6:06 pm
by Rocky5
Thank you will that a try, I tried the -w -e but it would change anything, the the script I had made. .

Will let you know how it does.

Works thank you :-) it doesn't work if you add an output file, this is here I was going wrong.

Re: Little Endian to Big Endian

Posted: Fri Sep 27, 2013 7:12 am
by howfie
if you still want to do it in C++. this is my code.

Code: Select all

/*
** BYTE ORDER FUNCTIONS
*/
#ifndef RC_INVOKED

template<class T>
inline void reverse_byte_order(T* data)
{
 unsigned char* ptr = reinterpret_cast<unsigned char*>(data);
 std::reverse(ptr, ptr + sizeof(T)); 
}

template<class T>
inline void reverse_byte_order(T* data, size_t elem)
{
 for(size_t i = 0; i < elem; i++) {
     unsigned char* ptr = reinterpret_cast<unsigned char*>(&data[i]);
     std::reverse(ptr, ptr + sizeof(T));
    }
}

#endif
usage

Code: Select all

 ifstream ifile; // open a file
 boost::shared_array<char> data; // load the file data into here
 size_t filesize; // load the file size into here

 reverse_byte_order(reinterpret_cast<unsigned short*>(data.get()), filesize/sizeof(unsigned short)); // two-byte swap
 reverse_byte_order(reinterpret_cast<unsigned*>(data.get()), filesize/sizeof(unsigned)); // four-byte swap
pretty useful in converting DDS textures that need to be byte-swapped.

Re: Little Endian to Big Endian

Posted: Tue Oct 08, 2013 4:58 am
by Sir Kane
Or use http://msdn.microsoft.com/en-us/library/a3140177.aspx which results in far less instructions per swap.

Re: Little Endian to Big Endian

Posted: Fri Oct 11, 2013 1:31 pm
by Malvineous
I have a C++ utility header that can also do this.

Code: Select all

#include "byteorder.hpp"
int val = be16toh(1234); // be16toh = big-endian 16-bit to host

std::fstream file("test.dat");
uint16_t value;
file >> u16be(value); // read 16-bit big-endian value from file stream

Re: Little Endian to Big Endian

Posted: Thu Mar 13, 2014 2:39 pm
by twisted
010 Editor

Tools > Hex Operations > Swap