Byte-swap tool for Windows?

You can talk about almost anything that you want to on this board.

Moderator: Moderators

Post Reply
SuperWill24
Posts: 31
Joined: Sat May 13, 2017 7:54 pm

Byte-swap tool for Windows?

Post by SuperWill24 » Sun May 06, 2018 10:02 am

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.

Oziphantom
Posts: 774
Joined: Tue Feb 07, 2017 2:03 am

Re: Byte-swap tool for Windows?

Post by Oziphantom » Sun May 06, 2018 10:11 am

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.

lidnariq
Posts: 8765
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: Byte-swap tool for Windows?

Post by lidnariq » Sun May 06, 2018 10:31 am

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.

User avatar
pubby
Posts: 542
Joined: Thu Mar 31, 2016 11:15 am

Re: Byte-swap tool for Windows?

Post by pubby » Sun May 06, 2018 10:34 am

What are you actually trying to do?

zzo38
Posts: 1050
Joined: Mon Feb 07, 2011 12:46 pm

Re: Byte-swap tool for Windows?

Post by zzo38 » Sun May 06, 2018 11:45 am

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)
[url=gopher://zzo38computer.org/].[/url]

User avatar
TmEE
Posts: 712
Joined: Wed Feb 13, 2008 9:10 am
Location: Estonia, Rapla city (50 and 60Hz compatible :P)
Contact:

Re: Byte-swap tool for Windows?

Post by TmEE » Sun May 06, 2018 1:53 pm

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.

User avatar
Zutano
Posts: 38
Joined: Tue Apr 04, 2017 1:22 pm
Location: Ohio, USA
Contact:

Re: Byte-swap tool for Windows?

Post by Zutano » Tue May 08, 2018 7:42 am

You can do this pretty easily with Python (free and open source) with something like this for 16-bit words:

Code: Select all

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: Select all

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

Post Reply