A friend came to me the other day asking if I knew how to convert hex strings to ASCII. For some backstory, the e-mail server they use at his workplace (Softalk Mail Server) wasn’t archiving e-mail properly, so he had about 2 years’ worth of mail to delete from the server. Problem is, the server stores the location of those e-mails in a folder hierarchy on the server as .eml files, with a file path for each stored in a SQL database for reference.
He was able to dump those reference paths from the database, but the problem was that they were all in hex. As in:
433A5C446F63756D656E747320616E6420[ ... ]70334230342E656D6C
So, he had a text file with about 275,000 lines of hex strings representing a file path, with groups of 2 characters in the string representing a single character in the path (such as “43″ representing C). I decided a simple Powershell script could do the trick, so I wrote just that. It gets the content of a text file with multiple lines of hex strings, puts them into an array, then loops through, concatenating them in groups of 2 and converting them with a C# method. It then appends it into an output string.
I present that code below, released under theĀ WTFPL, so feel free to use and/or modify at your own risk. I realize that it’s a pretty narrow-minded script, and pretty limited to Softalk Mail Server users, but with some modification it should be able to do general hex to ASCII conversions:
# SOFTTALK E-MAIL SERVER HEX FILENAME PATH CONVERTER
# Script written by Adam DiFrischia 2010
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004
#
# Copyright (C) 2004 Sam Hocevar
#
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
# 0. You just DO WHAT THE FUCK YOU WANT TO.
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
# Ask for the path to the file with the lines of hex and get the content
$PathToFile = Read-Host "Input the file path for input"
$hex = gc $PathToFile
# Ask for converted file path output
$OutputPath = Read-Host "Input the file path for output"
$hex | foreach{ # Print out the contents of the hex file, and step through each line and perform a conversion
$output = $null # Clear the contents of $output, so that each loop through is fresh
$hexarray = $_.ToCharArray() # Take a line of hex and bust it down into an array of individual characters
$hexcount = $hexarray.count # Count how manhy items are in the array
$loopcount = 0 # Initialize Loop Counter
# Loop through the array, taking the item in the position of the array equal to the loop counter
# and the loop counter plus one, and concatenating them. Then, use C# methods to convert the concatenated value from HEX
# to CHAR and append it to a string. Each loop is subsequently appended. In my initial use case,
# this was for filenames, so it was a long line of hex with two hex values representing a single
# character in a filename. Trying to convert anything else with it verbatim will result
# in garbage and would need modified (this includes numbers).
while ($loopcount -le $hexcount -1) {
$currenthex = $hexarray[$loopcount] + $hexarray[$loopcount + 1]
$conversion = [Char][Convert]::ToInt32($currenthex,16)
$output = $output + $conversion
$loopcount = $loopcount + 2
}
$output | Out-File $OutputPath -append -noclobber # After each line of hex, append to an output file without overwriting.
}
Suggestions, tips, and flames welcome in the comments section below.
Works like a champ!