Monthly Archives: October, 2009

Reading binary data in C#

This caption is taken directly from an article i found here. The final result of this post is the following code:

/// <summary>
/// Reads data from a stream until the end is reached. The
/// data is returned as a byte array. An IOException is
/// thrown if any of the underlying IO calls fail.
/// </summary>
/// <param name="stream">The stream to read data from</param>
/// <param name="initialLength">The initial buffer length</param>
public static byte[] ReadFully (Stream stream, int initialLength)
{
    // If we've been passed an unhelpful initial length, just
    // use 32K.
    if (initialLength < 1)
    {
        initialLength = 32768;
    }
    
    byte[] buffer = new byte[initialLength];
    int read=0;
    
    int chunk;
    while ( (chunk = stream.Read(buffer, read, buffer.Length-read)) > 0)
    {
        read += chunk;
        
        // If we've reached the end of our buffer, check to see if there's
        // any more information
        if (read == buffer.Length)
        {
            int nextByte = stream.ReadByte();
            
            // End of stream? If so, we're done
            if (nextByte==-1)
            {
                return buffer;
            }
            
            // Nope. Resize the buffer, put in the byte we've just
            // read, and continue
            byte[] newBuffer = new byte[buffer.Length*2];
            Array.Copy(buffer, newBuffer, buffer.Length);
            newBuffer[read]=(byte)nextByte;
            buffer = newBuffer;
            read++;
        }
    }
    // Buffer is now too big. Shrink it.
    byte[] ret = new byte[read];
    Array.Copy(buffer, ret, read);
    return ret;
}

Charlie Winston – Buy before die

Not all european people might have heard this name before – but they should! His single first “Like a hobo” is more often played in the radio in the last days. And this time for a good reason: Its great! Not only the single – the whole CD “Hobo” is! And if u ask me: The song “Like a hobo” is not my favorite, but neverless its rocks like the whole album. It might be hard for you to listen more than the first 3 songs, cause u will repeat them endlessly!! So remember the name: Charlie Winston. Give it a try…

PS: I dont get paid for any commercial, im just enthusiastic 😉

.Net 3.* Language Extensions

A good site with examples for the language extension feature of .Net 3.* can be found here. As a shortcut look at this example taken from linked site:

public static bool In(this object o, IEnumerable c)
{
  foreach(object i in c)
  {
    if(i.Equals(o)) return true;
  }
  return false;
}

What the fuck is WUBI?!

The answer is as easy as the program itself: Its an ubuntu installer for windows. If u are a windows user but sometimes u need a linux u could install yourself a virtual machine.

I used VirtualBox for a long time – it was great but it was slow! As i decided to switch to Windows 7 i also decided to try an alternativ and what i found was WUBI. And now im happy: Its easier to install than a virtual machine, no need for an extra partition and its fast! The only sad thing: Switching between Windows and Ubuntu needs a computer restart…

RoboRally – Roboter meets AI

A friend sent me an email with a link to a little programming contest. The aim is to program an AI for a roboter that should find a way from the origin to the destination – the rules are deeply inspired by the boardgame RoboRally. The reward may not be worth to invest time at all but to see a little robot moving around the holes and splippering over oil is great fun – u always hope that he might be clever enough to reach his aim and dont stuck… so, do it!

Binary Search Tree and Red-Black Tree in C#

Willing to implement some missing(?) base functionality in my own library to extend .NET i found these articles: Working with Red-Black Trees in C#, An Extensive Examination of Data Structures and Implementing a Red-Black Tree in C#.
While the first seems to be exactly what i was looking for, the second article on MSN provides some additional reading about datastructures. The sourcecode examples are somehow outdated cause they are written for .NET 2.0 but neverless its quite basic knowledge for everyone well explained. The last article reminds me at some point how i would have done it 10 years before 😉 I didnt read it at all, just got a quick look at the sources and my first thought was: Too much lines of code for a Red-Black Tree! But i should give it a second try – also the rest of the blog holds some extensive examples of different programming patterns. And you can never know enough about it…

Nice site for guitar players – lyrics and chords

Found this site today: http://www.ultimate-guitar.com/
Just bookmarked it but im to afraid of loosing the link by upgrading the browser or reinstall the OS ;), so i just added a new category and voila – here it is.