Click here to Skip to main content
13,095,218 members (77,248 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 13 Jun 2006


, 13 Jun 2006
Rate this:
Please Sign up or sign in to vote.
Convert ASCII text to binary and back.

Sample Image - TextToBinaryAndBack.jpg


Binnary Converter is a fairly simple program that does pretty much what it says. I am aware that the spelling of Binnary is wrong...but it got some good laughs in the lounge so I decided to leave it alone :)-.

To convert text to binary, simply type your text into the top text box and press the Convert button. Your text will be converted to binary and displayed in the second text box.

To convert binary to text, select the radio button titled 'Binary To String', enter your binary into the top text box (do not use spaces etc...only 1's and 0's please ;)), and press the Convert button. Your binary will be converted to text in the bottom text box.

The main functions for this program are IntToBin and BinToInt8.

IntToBin is an optimization of a standard routine for converting numbers to binary and is not too complex:

Private Shared Function IntToBin(ByVal Number As Integer) As String
    Dim Temp As Integer = 1
    Do Until Temp > Number
        Temp <<= 1'Quickly multiply by 2 by shifting up one bit
    While Temp > 0
        If Number < Temp 
            Then : IntToBin &= "0"
            IntToBin &= "1"
            Number -= Temp
        End If
        Temp >>= 1'Quickly divide by 2 by shifting down 1 bit
    End While
    IntToBin = IntToBin.PadLeft(8, "0")'Pad to make this 1 byte (8 bits)
End Function

BinToInt8 is also a simple routine that uses a reverse lookup to calculate the numeric value of an 8 bit binary string:

Private Shared m_IndexArr() As Integer = {128, 64, 32, 16, 8, 4, 2, 1}

Private Shared Function BinToInt8(ByVal chars() As Char) As Integer
    For i As Integer = 0 To 7
        If chars(i) = "1" Then BinToInt8 += m_IndexArr(i)
    Next i
End Function

This was built more as a toy than for a practical purpose, but I have put the conversion routines into a separate class for you just in case you would like to use it in your project. I hope you enjoy it and that it brings you some fun during those times at the office when you are stumped and need a good distraction ;)

If you have any questions or suggestions, please feel free to ask them.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
United States United States
PC Programmer/Analyst

You may also be interested in...

Comments and Discussions

QuestionImplementation In Excel Pin
John Gladstone31-Aug-06 3:39
memberJohn Gladstone31-Aug-06 3:39 
AnswerRe: Implementation In Excel Pin
Polymorpher31-Aug-06 8:36
memberPolymorpher31-Aug-06 8:36 
General:) Pin
Polymorpher14-Jun-06 2:41
memberPolymorpher14-Jun-06 2:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170813.1 | Last Updated 13 Jun 2006
Article Copyright 2006 by Polymorpher
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid