Edit binary files in Vi

If you've ever wanted to examine or edit a binary file in your favorite text editor, there's an easy way to simulate a vi hex mode. To do this, you just filter the file's contents through the xxd hex dump utility, a trick that can be accomplished right within the vi/vim interface.
To convert a file to hex dump representation, just load your file in vi and type the following:
:%!xxd
This sends the entire contents of the opened document to xxd and loads in the result. At this point, you can view or edit any of the hex data. The ASCII representation is listed to the right, though editing this region will not affect the hex portion of the file.
When you are done, you'll want to convert things back into their binary format before saving. To do this, you run things through xxd again, but this time with the -r option:
:%!xxd -r
Your file should be returned to illegible gibberish, which you can save back out with :wq.
A funny thing I just noticed: OS X binaries all start with the same 4 bytes which, in hex, spell out the phrase "cafe babe". This is just a magic number used to identify the file as an OS X binary, but it's hard not to ascribe some deeper meaning. ;)
Posted by Jason Striegel |
Aug 9, 2008 10:00 PM
Linux, Mac |
Permalink
| Comments (5)
Recent Entries
- iPhone screen capture
- Wii DVD player
- Korg Kaossilator 4-bar loop hack
- HOWTO - Protect GMail from session snatching
- 10 Google Apps Tips
- Super Mario World and quantum physics
- HOWTO fly in the US without a wallet
- Run homebrew games on the PSP Slim
- Edit binary files in Vi
- Dell Inspiron 1525 HackBook Pro
Comments
Newest comments listed first.
| Posted by: peder on August 10, 2008 at 5:43 AM |
cafebabe is actually the file magic number of java class files, that suggests that OS X binaries actually are java-programs.
Btw, look at the unix utility 'file' for many more such magic numbers. A typical exe on windows will start with MZ, the initials of Mark Zbikowski, one of the developers of MS-DOS. Binaries on linux start with 0x7f ELF. Most file-formats have such magic numbers.
| Posted by: Bader on August 10, 2008 at 12:30 PM |
How is cafebabe represented in just 4 bytes?
| Posted by: Bader on August 10, 2008 at 12:36 PM |
my bad I see it now, I thought it meant 99, 97, 102, 101, 98, 97, 98, 101
| Posted by: Tom on August 14, 2008 at 12:30 PM |
I came accross bvi the other day,
just: sudo apt-get install bvi
then use bvi on your binary file
| Posted by: Anonymous Coward on August 16, 2008 at 5:33 PM |
0xCAFEBABE and it's relatives (0xDEADBEEF, etc) are used not to determine the executable format but the endianness of the running computer.
If you look through a universal binary you'll see other magic numbers in the file. One 0xBEBAFECA is the same but for the PPC architecture.
Leave a comment
Bloggers
Welcome to the Hacks Blog!
Categories
- Ajax
- Amazon
- AppleTV
- Astronomy
- BlackBerry
- Blogging
- Body
- Cars
- Cryptography
- Data
- Design
- Education
- Electronics
- Energy
- Events
- Excel
- Excerpts
- Firefox
- Flash
- Flickr
- Flying Things
- Food
- Gaming
- Gmail
- Google Earth
- Google Maps
- Government
- Greasemonkey
- Hacks Series
- Hackszine Podcast
- Halo
- Hardware
- Home
- Home Theater
- iPhone
- iPod
- IRC
- iTunes
- Java
- Kindle
- Knoppix
- Language
- LEGO
- Life
- Lifehacker
- Linux
- Linux Desktop
- Linux Multimedia
- Linux Server
- Mac
- Mapping
- Math
- Microsoft Office
- Mind
- Mind Performance
- Mobile Phones
- Music
- MySpace
- MySQL
- NetFlix
- Network Security
- olpc
- OpenOffice
- Outdoor
- Parenting
- PCs
- PDAs
- Perl
- Philosophy
- Photography
- PHP
- Pleo
- Podcast
- Podcasting
- Productivity
- PSP
- Retro Computing
- Retro Gaming
- Science
- Screencasts
- Security
- Shopping
- Skype
- Smart Home
- Software Engineering
- Sports
- SQL
- Statistics
- Survival
- TiVo
- Transportation
- Travel
- Ubuntu
- Video
- Virtualization
- Visual Studio
- VoIP
- Web
- Web Site Measurement
- Windows
- Windows Server
- Wireless
- Word
- World
- Xbox
- Yahoo!
- YouTube
Archives
Recent Posts
- iPhone screen capture
- Wii DVD player
- Korg Kaossilator 4-bar loop hack
- HOWTO - Protect GMail from session snatching
- 10 Google Apps Tips
- Super Mario World and quantum physics
- HOWTO fly in the US without a wallet
- Run homebrew games on the PSP Slim
- Edit binary files in Vi
- Dell Inspiron 1525 HackBook Pro
www.flickr.com
|




