473,378 Members | 1,209 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

converting problem from C++ to C#

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
0 1998

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

Similar topics

4
by: mustafa | last post by:
Dear sir , I have built my application in visual basic 6.0 and crystal Report8.5 , Now i migrated my application to VB.net using the upgrade wizard.My visual basic form is upgraded to vb.net...
29
by: Armand Karlsen | last post by:
I have a website ( http://www.zen62775.zen.co.uk ) that I made HTML 4.01 Transitional and CSS compliant, and I'm thinking of converting it into XHTML to learn a little about it. Which XHTML variant...
8
by: prabha | last post by:
Hello Everybody, I have to conert the word doc to multiple html files,according to the templates in the word doc. I had converted the word to xml.Also through Exsl ,had finished the multiple...
9
by: Edward Diener | last post by:
I received no answers about this the first time I posted, so I will try again. My inability to decipher an MSDN topic may find others who have the same inability and someone who can decipher and...
5
by: Robert | last post by:
I have a series of web applications (configured as separate applications) on a server. There is a main application at the root and then several virtual directories that are independant...
7
by: Tor Aadnevik | last post by:
Hi, I have a problem converting values from Single to double. eg. When the Single value 12.19 is converted to double, the result is 12.1899995803833. Anyone know how to avoid this? Regards...
4
by: gg9h0st | last post by:
i'm a newbie studying php. i was into array part on tutorial and it says i'll get an array having keys that from member variable's name by converting an object to array. i guessed "i can...
4
by: Bob Alston | last post by:
Anyone have experience with converting an access app from Jet database to Mysql? I am specifically looking for any changes I would have to make to my access forms, queries, modules, vba code, etc....
7
by: Coleen | last post by:
Does anyone have any good detailed information on the conversion process? We are in the process of converting 2 projects from 2003 to 2005 and have some conversion errors that I can not find...
2
by: shenanwei | last post by:
DB2 V8.2 on AIX, type II index is created. I see this from deadlock event monitor. 5) Deadlocked Connection ... Participant no.: 2 Lock wait start time: 09/18/2006 23:04:09.911774 .........
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.