C# algorithm for aesthetically converting bytes to strings

A quick little C# algorithm for converting bytes to strings. Pretty self-explanatory, but it may come in handy for someone sometime. This basically wraps the in-built Convert.ToString() method within C#, and “beautifies the result a little – adding 0s to the front of it to make the resultant string a full 8 characters long.

public static string b2s(byte b) {
string result = Convert.ToString(b, 2);
while(result.Length < 8 ) result = "0" + result;
return result;
}

Nothing revolutionary at all, but it provides a nicer looking output and may come in handy for someone to use.

Advertisements

About Cameron
I'm a final year Computer Science/Information Systems major. Already finished my BA in Politics/Philosophy. I do web and software freelance on the side, while I finish studying. Hoping to be self-employed by the end of my degree, otherwise off into the real-world I go....

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: