By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,570 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,570 IT Pros & Developers. It's quick & easy.

converting problem from C++ to C#

P: 1
Hi all Iíve some problem, Iím trying to converting a sorting algorithm from C++ to C#.
In C++ Iíve compiled it using MSVC and its working properly, and in C# Iím using .NET Framework 2.0 (Visual Studio 2005).

The problem occurred when I trying to opening and reading file:

=============
C++
=============

void LoadSourceFile(char * fileName, unsigned char * data, int length)
{
// loads the source file into a buffer.
data = 0;
length = 0;

FILE * fp = fopen(fileName, "rb");
if (fp == 0)
return;

fseek(fp, 0, SEEK_END);
length = ftell(fp);
fseek(fp, 0, SEEK_SET);

if (length < 1)
return;

data = (SYMBOL_TYPE *)(new char[length]);

fread(data, 1, length, fp);

// for debug check only
// the dimension for data2 is only an example
unsigned char data2[1000];
fread(data2, length, fp)

fclose(fp);

}

============
C#:
============

void LoadSourceFile(char[] fileName, ref char[] data, ref int fileSize)
{
// get the full path of file in string
string strFileName = new string(fileName);
// get the size of file (in bytes)
FileInfo fInfo = new FileInfo(strFileName);
fileSize = (int)fInfo.Length;

// check file size
if (fileSize < 1)
return;

// initialize FileStream and StreamReader object
FileStream file = null;
StreamReader sReader = null;

file = new FileStream(strFileName, FileMode.Open, FileAccess.Read);

sReader = new StreamReader(file, Encoding.UTF7);

// initialize array of char based on file size
data = new char[fileSize];

sReader.Read(data, 0, fileSize);

}

The problem is when I debug and check the values in variable data2 in C++ and variable data in C# the result is different, not totally different but thereís some element have different value. The problem donít occurred if Iím only opening an ASCII text file.

Anyone know why this problem occurred? Iíve try different encoding on StreamReader but still there isnít encoding that give a identical value in array when I debug.

**********

The second problem:

C++:

unsigned short Value16(unsigned int sourceIndex)
{
return *(unsigned short*)(m_source + sourceIndex)
}
// used here:
unsigned short test = Value16(m_sourceLength Ė 2);

C#:

unsafe ushort Value16(uint sourceIndex, char* m_source)
{
return *(ushort*) (m_source + sourceIndex)
}

// used here:
ushort test = Value16((m_sourceLength - 2), m_source);

Note:
m_source is a pointer to variable data on the first problem above.

When I debug the result for variable test is totally different for both. I donít know why this happen.

Thanks for your attention, if youíre not busy and have spare time please help me, I really need your help.
Nov 16 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.