HOWTO: Convert binary to human readable text

This is a quick fun little HOWTO.  The was an article on CollegeHumor today that looked like this:

image

01010100 01101000 01100101 00100000 01110010 01101111 01100010 01101111 01110100 01110011 00100000 01100001 01110010 01100101 00100000 01100001 01101101 01101111 01101110 01100111 00100000 01110101 01110011 00100000 01100001 01101110 01100100 00100000 01110111 01100101 00100000 01100001 01110010 01100101 00100000 01100001 01101100 01101100 00100000 01101001 01101110 00100000 01100100 01100001 01101110 01100111 01100101 01110010 00101110 00100000 01001000 01100101 01101100 01110000 00100000 01110101 01110011 00101100 00100000 01000111 01101111 01100100 00101110

I was of course curious what the message above actually said so I wondered how quickly I could figure that out with PowerShell.  The answer?  Pretty gosh darn quick.

((gc c:\temp\binary.txt) -split " " | % { [char]([convert]::ToInt32("$_",2)) }) -join ""

I copied the binary into a file and loaded that simply so the code above would visually fit into one line.  I’ve also intentionally used short form syntax. In full form that looks like:

# Load in our binary data with each byte separated by a space
$InputBinaryData = Get-Content -Path c:\temp\binary.txt

# Crate a new array which each element in the array is one of the bytes from the file
$BinaryDataAsArray = $InputBinaryData -split " "

# Create a new array that will store the converted ASCII values for each byte
$AsciiArray = @()

# Loop through one at a time each of the bytes extracted from the input file
ForEach($Byte in $BinaryDataAsArray)
{
    # Convert the byte to an integer (For example 1000001 as a byte is 65 as an integer)
    $ThisByteAsInteger = [convert]::ToInt32("$Byte",2)
    
    # Next convert this integer to an ASCII (aka human alphabet) (For example, 65 as an integer is a capital 'A' in ASCII)
    $ThisIntegerAsAscii = [char]$ThisByteAsInteger
    
    # Take this English letter and add it to out output array and then go onto the next one
    $AsciiArray += $ThisIntegerAsAscii
}

# Once we've collected and converted all of the bytes, we join the string together on a blank (meaning every character will get placed on one line)
$FinalString = $AsciiArray -join ""

# We then write the final converted English string to the screen
Write-Output -InputObject $FinalString

Oh and as for the message?  Well I’ll leave that for you to decode.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.