How to register a global hotkey for your application in C#

How to register a global hotkey for your application in C#

Register a hotkey for your application that can be triggered even if your form is minimized and does not have focus. More »

Visual Studio 2013

Visual Studio 2013

Visual Studio 2013 is now available. Make sure to upgrade to the latest version of Visual Studio. More »

How to download a file in C# (progressbar and download speed)

How to download a file in C# (progressbar and download speed)

Learn how to download files in C# while displaying the percentage and the download speed. More »

UDP hole punching implementation in C#

UDP hole punching implementation in C#

Learn how to implement UDP hole punching so you can make your clients life a lot easier by not forcing them to open ports on their end. More »

 

Convert string to binary and binary to string in C#

The following two snippets allow you to convert a string to binary text and also to convert binary back to string.

String to binary method:

Binary to string method:

Usage:

Share on FacebookTweet about this on TwitterShare on Google+Share on StumbleUponShare on LinkedInShare on RedditPin on PinterestShare on TumblrDigg thisPrint this pageEmail this to someone

6 Responses to Convert string to binary and binary to string in C#

  1. Dmitriy says:

    Binary to string method doesn’t work with cyrillic..

  2. Raymond says:

    These methods do the job but aren’t efficient. And they assume the input you wish to convert to binary is an integer — this character parsing chokes on types whose string representation includes a decimal point (float, double, decimal). If you know the numerical type (int, uint, long, ulong, etc.) just use .NET’s built-in BCL Convert.ToString() method. For example:

    For the sake of curiosity I wrote a quick test to compare StringToBinary() with Convert.ToString() for myLongNumber, each running for 1 million iterations, running on a Core 2 Duo w/ 3GB RAM. StringToBinary() takes an average of 3818 milliseconds while Convert.ToString() takes an average of 955 milliseconds.

    You might be thinking that StringToBinary() is superior because it accepts numerical values of arbitrary size and there fore generates arbitrary-length binary string representations of those values, but the performance is a big penalty to pay. Additionally, BinaryToString() should validate that the parameter ‘data’ passed into the method doesn’t contain invalid characters (anything other than “0” and “1”). This validation would further slow performance.

    More generally, because StringToBinary() accepts a string-formatted representation of a numerical value (that isn’t validated) you’re losing all the benefits of strong typing in C#. Unless you have a very good reason for needing this kind of “flexification” — and perhaps there are a few edge or corner cases — I recommend avoiding these approaches.

    For the curious: my test harness code:

  3. Sowren Sen says:

    Very helpful. Nice work. Thanks.

  4. hGaR says:

    Several thanks for the wonderful post

  5. jednoraz says:

    Terrific work! This is the type of info that should be shared around the net. Shame on the search engines for not positioning this post higher!

  6. Robby Applegate says:

    Hey there, You’ve done a great job. I will definitely digg it and personally suggest to my friends. I’m confident they
    will be benefited from this site.

Leave a Reply

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