30

Take this example:

i = 0x12345678
print("{:08x}".format(i))
   # shows 12345678
i = swap32(i)
print("{:08x}".format(i))
   # should print 78563412

What would be the swap32-function()? Is there a way to byte-swap an int in python, ideally with built-in tools?

Patrick B.
  • 11,773
  • 8
  • 58
  • 101
  • 4
    Alternatively, the [`array.byteswap()`](https://docs.python.org/2/library/array.html) method if you first convert the number into a byte-array – aruisdante Dec 16 '14 at 14:10
  • 3
    @aruisdante I found this question, but its title does not correspond to what is asked by the OP.... changed its title. – Patrick B. Dec 16 '14 at 14:12
  • Your example is swapping the order of 4-bit chunks, not bytes. If you swapped the order of the four bytes, `{ 12 34 56 78 }`, it would be `{ 78 56 34 12 }`. – AaronF Feb 05 '19 at 21:40
  • @AaronF what? Your "it would be" is exactly what I put in the comment. – Patrick B. Feb 07 '19 at 13:02
  • My bad. I misread it has 87654321. Sorry about that. You're right. – AaronF Feb 07 '19 at 18:10

5 Answers5

34

One method is to use the struct module:

def swap32(i):
    return struct.unpack("<I", struct.pack(">I", i))[0]

First you pack your integer into a binary format using one endianness, then you unpack it using the other (it doesn't even matter which combination you use, since all you want to do is swap endianness).

Carsten
  • 17,991
  • 4
  • 48
  • 53
  • What should be the result of swap32(12345678) ? – Shahriar Dec 16 '14 at 14:35
  • 1
    @AerofoilKite be careful, don't be confused by the print of `{:08x}`, it is omitting the `0x`. 12345678 != 0x12345678 – Patrick B. Dec 16 '14 at 15:11
  • 3
    @Carsten, "i" format for functions pack and unpack means signed integer. As a result, value like 0x87654321 is not handled well with your definition. To treat values as unsigned, format "I" could be used: `struct.unpack("I", i))[0]` – Artem Zankovich Mar 10 '16 at 02:21
  • @ArtemZankovich Thank you! I forgot about that. I have updated my answer accordingly. – Carsten Mar 10 '16 at 09:25
31

Big endian means the layout of a 32 bit int has the most significant byte first,

e.g. 0x12345678 has the memory layout

msb             lsb
+------------------+
| 12 | 34 | 56 | 78|
+------------------+

while on little endian, the memory layout is

lsb             msb
+------------------+
| 78 | 56 | 34 | 12|
+------------------+

So you can just convert between them with some bit masking and shifting:

def swap32(x):
    return (((x << 24) & 0xFF000000) |
            ((x <<  8) & 0x00FF0000) |
            ((x >>  8) & 0x0000FF00) |
            ((x >> 24) & 0x000000FF))
nos
  • 223,662
  • 58
  • 417
  • 506
18

From python 3.2 you can define function swap32() as the following:

def swap32(x):
    return int.from_bytes(x.to_bytes(4, byteorder='little'), byteorder='big', signed=False)

It uses array of bytes to represent the value and reverses order of bytes by changing endianness during conversion back to integer.

Artem Zankovich
  • 2,319
  • 20
  • 36
4

Maybe simpler use the socket library.

from socket import htonl

swapped = htonl (i)
print (hex(swapped))

that's it. this library also works in the other direction with ntohl

RobD
  • 91
  • 1
  • 7
1

The array module provides a byteswap() method for fixed sized items. The array module appears to be in versions back to Python 2.7

array.byteswap() “Byteswap” all items of the array. This is only supported for values which are 1, 2, 4, or 8 bytes in size;

Along with the fromfile() and tofile() methods, this module is quite easy to use:

import array

# Open a data file.
input_file = open( 'my_data_file.bin' , 'rb' )

# Create an empty array of unsigned 4-byte integers.
all_data = array.array( 'L' )

# Read all the data from the file.
all_data.fromfile( input_file , 16000 )               # assumes the size of the file

# Swap the bytes in all the data items.
all_data.byteswap( )

# Write all the data to a new file.
output_file = open( filename[:-4] + '.new' , 'wb' )    # assumes a three letter extension
all_data.tofile( output_file )

# Close the files.
input_file.close( )
output_file_close( )

The above code worked for me since I have fixed-size data files. There are more Pythonic ways to handle variable length files.

noodle7
  • 21
  • 1
  • 4