bswap - Linux


Overview

bswap is a command-line utility used to swap the bytes in a value or file, effectively changing the endianness. It finds use in various scenarios, such as:

  • Converting between big-endian and little-endian systems
  • Reversing the order of bytes for network communication
  • Manipulating binary data formats

Syntax

bswap [options] [value]
bswap [options] -f <file>

Options/Flags

  • -h, –help: Display usage information
  • -v, –version: Show version information
  • -f, –file : Perform byte swapping on the specified file instead of a value
  • -i, –inplace: Modify the file in-place instead of creating a copy
  • -s, –size : Swap bytes in blocks of the specified size (default: 4)

Examples

Simple byte swapping:

$ bswap 0x12345678
0x78563412

Swapping bytes in a file:

$ bswap -f data.bin

Swapping bytes in a file with a specific block size:

$ bswap -f data.bin -s 8

Common Issues

  • Endianness mismatch: If the input value or file is not in the expected endianness, the output will be incorrect.
  • File permissions: You must have write permissions to modify files in-place.

Integration

Convert a big-endian integer to little-endian:

$ printf "%08x" `bswap $(printf '%08x' 0x12345678)`
0x78563412

Reverse the order of bytes in a binary file:

$ bswap -f image.bin

Related Commands

  • set: Set the value of a variable
  • printf: Print formatted output
  • hexdump: Dump memory in hexadecimal format