473,738 Members | 4,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

converting problem from C++ to C#

1 New Member
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(strFil eName);
fileSize = (int)fInfo.Leng th;

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

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

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

sReader = new StreamReader(fi le, Encoding.UTF7);

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

sReader.Read(da ta, 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(unsigne d int sourceIndex)
{
return *(unsigned short*)(m_sourc e + sourceIndex)
}
// used here:
unsigned short test = Value16(m_sourc eLength – 2);

C#:

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

// used here:
ushort test = Value16((m_sour ceLength - 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 2031

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

Similar topics

4
2470
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 but my .dsr file that i created using crystalReport is not migrated , when i try to convert it into .rpt file using add new item in Vb.net Envs it give me the error in the log file saying,
29
3906
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 would you recommend? The w3c HTML validator mentions XHTML 1.0 Transitional, Basic, Strict, and XHTML 1.1. Would I be able to make my existing CSS work in the XHTML page without modification to the .css file?
8
5742
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 output html files. The problem is while reading through the worddoc paragraph,the special characters are not identified. So in the xml file,it's just storing that as "?".So I couldn't able to retrive the characters in my ouput html files.
9
2591
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 explain it. I have some questions about the instructions for creating a mixed mode DLL in the MSDN topic "Converting Managed Extensions for C++ Projects from Pure Intermediate Language to Mixed Mode" in the "Managed Extensions for C++ Reference"....
5
2525
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 applications. I am testing an upgrade of all of the sites and have converted the main root site...although not necessarily fixed any issues. I move on instead and converted one of the virtual roots that is a seperate
7
2707
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 Totto
4
3194
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 get public members but not protected, private, static members"
4
3321
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. I understand there are some data format differences in text field types and date types that need to be addressed if converting from Jet to Mysql. But other than things that have to be addressed when converting, what other changes must I make?...
7
2498
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 answers to. First, we have a lot of UserControls. I am getting the error "Type uc_mc_btn_footer is not defined." where uc_mc_btn_footer is the name of the UserControl. Also, on almost all of our UserControls the conversion has remmed out all my code...
2
5866
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 ...... Deadlocked Statement: Type : Dynamic Operation: Execute
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.