Reading a string as an int pointer

Ok, so it all started with my interest in hash codes. After some reading from a Jon Skeet post, I asked this question . This really interested me in pointer arithmetic that I have almost no experience. So, after reading this page, I started experimenting when I got a rudimentary understanding from there and my other fantastic peers here on SO!

Now I am doing a few more experiments, and I believe that I have exactly duplicated the hash code cycle, which is implemented stringbelow (I reserve the right to make mistakes):

Console.WriteLine("Iterating STRING (2) as INT ({0})", sizeof(int));
Console.WriteLine();

var val = "Hello World!";
unsafe
{
    fixed (char* src = val)
    {
        var ptr = (int*)src;
        var len = val.Length;
        while (len > 2)
        {
            Console.WriteLine((char)*ptr);
            Console.WriteLine((char)ptr[1]);

            ptr += 2;
            len -= sizeof(int);
        }

        if (len > 0)
        {
            Console.WriteLine((char)*ptr);
        }
    }
}

But, the results are a little perplexing to me; view. Here are the results:

Iterating STRING (2) as INT (4)

H
l
o
W
r
d

, ptr[1] , ( ) . . , ptr[1] 4 12 ?

+4
6

, int*.. 32 .. 16, char*.

, 32 . ( , ):

char * int * . ,

char, 16 .

int 32- . , ptr[0] H e ( H). ptr[1] - l s..

.

char :

Console.WriteLine((char)*ptr);

.. 16 , .

+12

http://msdn.microsoft.com/en-us/library/vstudio/x9h8tsay.aspx

A char - 16 , int - 32 . , 1 int ptr, 2 char .

.

+3

@ - .

, , . , , - int *.

var val = "Hello World!";
/*
           Hello World!
char idx = 012345678911
                     01

           Hello World!
int idx =  0 1 2 3 4 5

-> this is why len should be 6 below    

*/
unsafe
{
    fixed (char* src = val)
    {
        var ptr = (int*)src;

        //explicit definition of what val.Length / 2 would actually mean
        // -> there are actually 6 integers here but 12 chars
        var len = val.Length * sizeof(char) / sizeof(int);  
        while (len > 0)
        {
            //char pointer to the first "char" of the int
            var word = (char*) ptr;         
            Console.WriteLine(*word);
            /* types matter here.  ptr[1] is the next _integer_ 
               not the next character like it would for a char* */
            Console.WriteLine(word[1]);   //next char of the int @ ptr

            ptr++; // next integer / word[2]
            len--;
        }
    }
}
+3

# 2 , UTF16.

+2

char - 16 , int - 32, 32 . , int short (16 ). Hello

var ptr = (short*)src;
+1

, :

#, 2 (16 ). , - 4 (32 ). UP TO 2 ^ 16, 16 , , UTF-16. , .

Be that as it may, the difference in size. Int is 4 bytes for characters 2, therefore, increasing (like your pointer int) in units SizeOf(Int)(4Bytes) rather than Char or Byte * 2, you move 32 bits by reading 16 and then skip ahead another 32, forcing you to skip all the rest char. Therefore, HLOWR D.

If you want to learn more about pointer articometry and bitwise operations, learning some basic C is cool and pretty fun (discussion subject).

0
source

All Articles