binhex4 Encoding/Decoding using Python

Python’s binhex4 module can encode and decode files in binhex4 format which is a binary-to-text encoding system that is used in Mac OS for sending files across a network. For example – For sending some file from Mac OS via email, it firstly will convert file to binhex4 format and send it across network. Primarily this conversion(to binhex4) is implemented in Mac for preventing file being getting corrupted while going through a network.

In Python’s binhex module there are two functions binhex.binhex(input, output) and binhex.hexbin(input, output).

  • binhex.binhex(input, output) => Converts a file to binhex4 format
  • binhex.hexbin(input, output) => Decodes a binhex4 formatted file

Python Code for Converting File to binhex4 format

# Importing binhex Python Module
import binhex

# Importing sys Python Module for Reading File
import sys

# Setting name of file as f
f = "file.txt"

# Calling binhex function passing f and writing output to file.hqx
binhex.binhex(f, 'file.hqx')

Python Code for Decoding binhex4 Formatted File

# Importing binhex Python Module
import binhex

# Importing sys Python Module for Reading File
import sys

# Setting name of binhex4 formatted file as f
f = "file.hqx"

# Calling hexbin function passing f and writing output to file.txt
binhex.binhex(f, 'file.txt')

You can read about binhex Module Documentation here => binhex Encode/Decode binnhex4 Files

Do note that binhex Python Module have been deprecated from Python Version 3.9 onwards

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts