rewrite C function in C#

This is a discussion on rewrite C function in C# within the C# Programming forums, part of the General Programming Boards category; hello, how can i rewrite this code to completely managed? Code: unsigned int i, j, counter; unsigned char *ptr; unsigned ...

  1. #1
    Registered User khdani's Avatar
    Join Date
    Oct 2007
    Posts
    42

    rewrite C function in C#

    hello,
    how can i rewrite this code to completely managed?
    Code:
    	unsigned int i, j, counter;
    	unsigned char *ptr;
    	unsigned char buff[FILE_SIZE];
    	float array[25][41];
    	float f1, f2, f3;
    	FILE *fp;
    
    	fp = fopen("file.dat", "rb");
    	fread(buff, 1, FILE_SIZE, fp);
    	ptr = buff;
    	counter = 0;
    	for (i = 0; i < 41; i++)
    	{
    		for (j = 0; j < 25; j++)
    		{
    			if (!(counter & 0x1F))
    			{
    				ptr += 2;
    			}
    
    			array[j][i] = (*(float *)ptr);
    			ptr += 4;
    			counter++;
    		}
    	}
    thank you

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    First of all analyze it so you know what it's doing. Some things are rather confusing, like the counter.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Seems like an unusual function. I'm not too good with pointers, but maybe this might help:

    Code:
    using System;
    using System.IO;
    using System.Runtime.InteropServices;
    
    namespace TestApplication
    {
        class Program
        {
            static void Main()
            {
                byte[] buff = File.ReadAllBytes("file.dat");
                IntPtr ptr = GCHandle.Alloc(buff, GCHandleType.Pinned).AddrOfPinnedObject();
                float[,] array = new float[25, 41];
                int counter = 0;
                
                for (int i = 0; i < 41; i++)
                {
                    for (int j = 0; j < 25; j++)
                    {
                        if ((counter & 0x1f) == -1)
                            ptr = (IntPtr)((int)ptr + 2);
    
                        array[j, i] = (float)ptr;
                        ptr = (IntPtr)((int)ptr + 4);
                        counter++;
                    }
                }
            }
        }
    }
    Maybe someone with more experience would be able to say whether I'm way off the mark or not.

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    For completely managed:

    Code:
    	uint counter = 0;
    	
    	float[,] array = new float[25,41];
    
    	FileStream  fs = File.OpenRead("file.dat"); 	
    	BinaryReader br = new BinaryReader(fs); 
    
    	for (int i = 0; i < 41; i++)
    	{
    		for (int j = 0; j < 25; j++)
    		{
    			if (counter & 0x1F == 0)
    				br.ReadInt16();
    
    			array[j, i] = br.ReadSingle();
    			counter++;
    		}
    	}
    Of course you'll want to close the files after you are done with them, which was also omitted in your code.
    Last edited by Cat; 04-04-2009 at 09:15 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Registered User khdani's Avatar
    Join Date
    Oct 2007
    Posts
    42
    thank you

  6. #6
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Quote Originally Posted by khdani View Post
    hello,
    how can i rewrite this code to completely managed?

    Obviously you can't. Good luck.
    Here to Deceive, Inveigle, Obfuscate Since 1945

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04:33 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21