Tuesday, February 17, 2015

Possible combination two dimensional array

Here is how to find possible combination on one side of 5 by 5 Rubik’s cube with two colours. Mathematically two colours in 5 x 5 = 25 places comes to 225 = 3,35,54,432. To visualise this combination we can look from 1 to possible number of combination but challenge come on representing on Cube UI.

To achieve programmatically we need two dimension array representing 5 x 5 places on one face on cube. Imaging below face with yellow and white colour. Colour can be presented with numeric value 0 and 1. So, White = 0 and Yellow = 1




For example, to represent below face we need following values in 5 by 5 array

Index
0
1
2
3
4
0
0
0
0
0
0
1
0
0
0
0
0
2
0
0
0
0
0
3
0
0
0
0
0
4
0
0
0
0
1

Single number can be represented as 00000 00000 00000 00000 00001 which is nothing but 1

To achieve this using binary representation of each combination and transform into colour representation of each number.

The simple code will look like this, a represent binary string of each number starting from 225 to 1

for (long i = (long)Math.Pow(2, 25) - 1; i > 0; i--)
{
                a = Convert.ToString(i, 2).PadLeft(25, '0');
}

Full code to transform binary string to 5 by 5 and further into colour is attached in window application in this blog. This code is using TableLayoutPanel control to paint cube face.


Enjoy!

Thursday, January 8, 2015

C# String and UTF-8 Encoding

Few days back came across very interesting fact about C# string and its memory representation. As a developer distinguishing string based on encoding is not straight forward because Visual Studio debugger shows same string in debug window but internal memory representation is different. In normal scenario this can be ignored but plays vital roles while working with program that deals with string from different sources. This string input can be from user, file, stream, etc. This can be in any encoding. So, if program is doing string operation based on assumed encoding than problem starts.

To understand how C# internally stores string let us take an example,
Create text file in notepad and save as UTF-8 encoding.
Below code opens this file and reads content into string. As you see debugger shows string same as it written in file




So there are 4 characters in string. Let us look at the memory by adding this to Watch and converting in to Char Array. It shows 5 characters instead of 4



Now question is what is extra character? In simple terms it is indicator of encoding. So when transformation of string happens in various form then first char will be used to find source encoding, in this case UTF-8.

Ok, UTF-8 string has different Memory representation so what? How does it matter to developer. Well, it does when some string operations are performed on it. Let’s take an example of String.StartsWith().
In above code, adding one more line of code to check if string stars with “Te” and do a case insensitive comparison



What will be output of line # 2? True or False?
I was expecting True. But, answer is false

If you followed memory representation above then I am sure you might have figured out answer as False. What happens here is that .NET does binary sort and comparison internally. Since it has extra byte at beginning of the string and way UTF-8 chars are sorted, comparison fails.

To make StartsWith () work correctly in UTF-8 encoded string, change to StringComparison.InvariantCultureIgnoreCase

Hope this it was useful.