Binary Conversion (C#)

By Sorasyn on Dec 11, 2012

A small something I threw together to get a feel for the C# class library, keywords, and of course, Visual Studio 2012.

It's a small snippet that converts a string object into 8-bit binary sequences, and 8-bit sequences back into characters. A tag "b" is indicated at the end of a string to denote that the string is in binary form. It's methods are static so they must be called using the class name.

"Binary.ToBinary("Hello"); => 01001000 01100101 01101100 01101100 01101111b"

"Binary.ToBinary("H"); => "H" = 01001000b"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Binary
{
    class Program
    {
        static void Main(string[] args)
        {
        }

        //Converts the supplied string into separate 8-bit binary sequences.
        //"H" = 01001000b "Hello" = 01001000 01100101 01101100 01101100 01101111b
        //"b" is tagged at the end of each sequence to denote that the string is in binary.
        public static string ToBinary(string bin)
        {
            char[] c = bin.ToCharArray();
            string o = "", s = "";

            foreach (char ch in c)
            {
                int tmp = (int)ch;
                s = "";

                while (tmp > 1)
                {
                    s = Convert.ToString((tmp % 2)) + s;
                    tmp = (tmp / 2);
                }

                s = Pad(Convert.ToString(tmp) + s, 8);
                o += s + " ";
            }

            return o + "\bb";
        }//END Method ToBinary(string s);

        //Converts sequences back to strings
        //Sequences tagged with "b" will be treated as binary in 8 bit chunks.
        public static string ToString(string seq)
        {
            char[] c = seq.ToCharArray();
            string result = "";

            switch (c[c.Length - 1]) {
                case 'b':
                    string[] par = seq.Replace("b","").Split(' ');

                    foreach (string str in par) {
                        char[] tmp = str.ToCharArray();
                        int dec = 0;

                        for (int a = 0; a < tmp.Length; a++)
                        {
                            if (tmp[a].Equals('1')) {
                                dec += (int)Math.Pow( 2 , ( (tmp.Length - 1) - a ) );
                            }
                        }
                        result += Convert.ToString((char)dec);
                    }

                    break;
                default: break;
            }
            return result;
        }//END Method ToString(string seq);

        //Pads pure binary sequences to the specified length using 0's
        //"110" padded to a width of 4 would be "0110"
        private static string Pad(string bin, int width)
        {
            char[] c = bin.ToCharArray();
            if ( c.Length < width )
            {
                for (int a = 0; a < (width - c.Length); a++)
                {
                    bin = "0" + bin;
                }
            }
            return bin;
        }//END Method Pad(string s, int width);
    }
}

Comments

Sign in to comment.
MaSOuD   -  Dec 13, 2012

Yes, There's Mono project (An open source project) which enables us to run C# on Mac, Linux, etc.

 Respond  
Sorasyn   -  Dec 12, 2012

The "Mono" compatibility framework applies an environment that'll allow C# source code to compile to an executable that the native OS can understand. I'm not sure if it allows other .NET languages as well, but it's a neat project for development abroad. http://www.mono-project.com/Main_Page

 Respond  
Hawkee   -  Dec 12, 2012

Main problem is the apps will only run on Microsoft. Not good for Mac users.

 Respond  
Sorasyn   -  Dec 12, 2012

It's a surprisingly refreshing language given the similarities. Visual Studio is remarkably nice compared to what I had been using (TextPad, NetBeans, etc..), and is very nice in regards to creating desktop applications thus far. I'll still use Java for somethings, like Applets or others of the like, but C# is more acclimated to Windows (obviously), making many tasks easier.

 Respond  
MaSOuD   -  Dec 12, 2012

I see you gave C# a shot...
Now, how do you feel SunnyD? :P

 Respond  
Sorasyn   -  Dec 11, 2012

When converting a character to an integer value it's already in decimal. All this does is take that decimal value and sequence it by assigning a "1" at the index(s) which add up to the decimal number numerically.

 Respond  
[nas]peter   -  Dec 11, 2012

This looks interesting. What about a Decimal -> Binary converter?

 Respond  
Sorasyn   -  Dec 11, 2012

I'll give it a go.

http://msdn.microsoft.com/en-us/library/windows/apps/hh974581.aspx -- A thorough guide for making Windows Apps with what looks like any Windows based languages. VB, C#, C, C++, HTML/CSS/JS, XAML, etc..

 Respond  
Hawkee   -  Dec 11, 2012

I'll have to give it a try. Why don't you try building a simple Windows Store app? That'd probably be a good thing to get into if you're doing Windows development.

 Respond  
Sorasyn   -  Dec 11, 2012

VS Studio has some really nice tools for UI development of Windows Store apps. Templates, and grids of the like. That being said, I like how C# can work more interactively with Windows instead of being bottle-necked like Java seems to be, but that's to be expected since it IS developed by Microsoft. Lol.

 Respond  
Hawkee   -  Dec 11, 2012

Great. I'm tempted to get into this sort of thing sometime in the future. Hawkee was accepted into Microsoft Bizspark, so I have all of the Microsoft software at my disposal to create applications. Once we have an iPhone and Android app I may consider doing a Windows 8 app for Hawkee.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.