Important information: this site is currently scheduled to go offline indefinitely by end of the year.

Little Endian to Big Endian

Coders and would-be coders alike, this is the place to talk about programming.
Post Reply
Rocky5
beginner
Posts: 31
Joined: Wed Jun 20, 2007 11:30 pm
Has thanked: 1 time

Little Endian to Big Endian

Post 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.
User avatar
aluigi
VVIP member
VVIP member
Posts: 1916
Joined: Thu Dec 08, 2005 12:26 pm
Location: www.ZENHAX.com
Has thanked: 4 times
Been thanked: 664 times
Contact:

Re: Little Endian to Big Endian

Post 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
Rocky5
beginner
Posts: 31
Joined: Wed Jun 20, 2007 11:30 pm
Has thanked: 1 time

Re: Little Endian to Big Endian

Post 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.
howfie
double-veteran
double-veteran
Posts: 929
Joined: Fri Jul 08, 2011 12:06 pm
Location: Torrance, CA
Has thanked: 10 times
Been thanked: 274 times

Re: Little Endian to Big Endian

Post 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.
Sir Kane
veteran
Posts: 104
Joined: Mon Aug 06, 2012 4:14 am
Been thanked: 96 times

Re: Little Endian to Big Endian

Post by Sir Kane »

Or use http://msdn.microsoft.com/en-us/library/a3140177.aspx which results in far less instructions per swap.
Malvineous
beginner
Posts: 25
Joined: Sat Oct 16, 2010 5:34 am
Location: Brisbane, Australia
Has thanked: 1 time
Been thanked: 1 time
Contact:

Re: Little Endian to Big Endian

Post 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
twisted
veteran
Posts: 100
Joined: Mon Apr 23, 2007 11:25 pm
Has thanked: 2 times
Been thanked: 7 times

Re: Little Endian to Big Endian

Post by twisted »

010 Editor

Tools > Hex Operations > Swap
Post Reply