It is currently Tue May 22, 2018 3:09 am

All times are UTC - 7 hours





Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Sun May 06, 2018 10:02 am 
Offline

Joined: Sat May 13, 2017 7:54 pm
Posts: 27
Hi,
I understand this isn't necessarily related to the NES, but this is "General Stuff", right? :D

Basically, I've been in need of a program that can byte-swap files from start to end (big-endian to little-endian and vice versa). I am using a hex editor, but it doesn't seem to be able to do such a thing. This program should be free, and preferably open-source as well. Maybe you could program it yourself if you can't find any tool. I'm not good enough at coding yet.


Top
 Profile  
 
PostPosted: Sun May 06, 2018 10:11 am 
Offline

Joined: Tue Feb 07, 2017 2:03 am
Posts: 426
Okay lets imagine I have a file of 6 bytes

00 01 02 03 04 05

this is both little endian and big endian.

Okay lets imagine I have a file of 3 words

00 01 02 03 04 05
now let change the endian
01 00 03 02 05 04

Okay lets imagine I have a file of 2 longs

00 01 02 03 04 05
now lets change the endian
02 01 00 05 04 03

okay which way is the right way to flip it? Byte, Word or Long?
You have to understand the size of each element in the file and change the endian correctly for each item on an item per item basis. There is to my knowledge no tool to do this, as you have to make a file the specifies what each "item" is and then it has to parse and well that kind of something nobody really ever does without having the source code to read it already..

why do you need to change the endienes of a file? If you use something like Transhexlation it will convert a byte,word,long value from the hex in either Motorola or Intel so you can read it, but it won't actually change the hex file it self.


Top
 Profile  
 
PostPosted: Sun May 06, 2018 10:31 am 
Offline

Joined: Sun Apr 13, 2008 11:12 am
Posts: 7122
Location: Seattle
For the specific instance of byte-swapping 16-bit words (i.e. ABCD becomes BADC), the extremely ancient and open-source program dd can do that using conv=swab.


Top
 Profile  
 
PostPosted: Sun May 06, 2018 10:34 am 
Offline
User avatar

Joined: Thu Mar 31, 2016 11:15 am
Posts: 299
What are you actually trying to do?


Top
 Profile  
 
PostPosted: Sun May 06, 2018 11:45 am 
Offline
User avatar

Joined: Mon Feb 07, 2011 12:46 pm
Posts: 981
My program utftovlq can do that; use utftovlq wW to swap endianness of 16-bit words (you can also swap 32-bit words with utftovlq dD, but not 24-bit) (Windows executable is not provided, but you could compile it yourself)

_________________
.


Top
 Profile  
 
PostPosted: Sun May 06, 2018 1:53 pm 
Offline
User avatar

Joined: Wed Feb 13, 2008 9:10 am
Posts: 636
Location: Estonia, Rapla city (50 and 60Hz compatible :P)
Oziphantom wrote:
why do you need to change the endienes of a file? If you use something like Transhexlation it will convert a byte,word,long value from the hex in either Motorola or Intel so you can read it, but it won't actually change the hex file it self.


All the MD ROMs have reverse byte order compared to what pretty much all EPROM programmers expect.

_________________
http://www.tmeeco.eu


Top
 Profile  
 
PostPosted: Tue May 08, 2018 7:42 am 
Offline
User avatar

Joined: Tue Apr 04, 2017 1:22 pm
Posts: 29
Location: Ohio, USA
You can do this pretty easily with Python (free and open source) with something like this for 16-bit words:
Code:
with open('input.bin', 'rb') as input_file, open('output.bin', 'wb') as output_file:
   data = input_file.read()
   for i in range(0, len(data), 2):
      high = data[i]
      low = data[i + 1]
      output_file.write(low)
      output_file.write(high)


In fact, you can extend it to work with an arbitrary word size like this:
Code:
input_file_name = 'input.bin'
output_file_name = 'output.bin'
word_size = 2

with open(input_file_name, 'rb') as input_file, open(output_file_name, 'wb') as output_file:
    while True:
        word = input_file.read(word_size)
        if not word:
            break
        output_file.write(word[::-1])

_________________
http://zutanogames.com/ <-- my dev blog


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group