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.
Important information: this site is currently scheduled to go offline indefinitely by end of the year.
Little Endian to Big Endian
- aluigi
- 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
you can try the -E option of quickbms that was added just for this job.
use the following script:
and then launch it with the following command:
the changes will be applied directly to the input file
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
Code: Select all
quickbms -w -E script.bms your_file.dds
Re: Little Endian to Big Endian
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.
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.
-
- 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
if you still want to do it in C++. this is my code.
usage
pretty useful in converting DDS textures that need to be byte-swapped.
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
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
Re: Little Endian to Big Endian
Or use http://msdn.microsoft.com/en-us/library/a3140177.aspx which results in far less instructions per swap.
-
- 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
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