问题
Could someone please help me to convert C# to C++? here is an example:
using System;
using System.Net;
using System.Text;
using System.IO;
using System.Threading;
namespace read_website
{
class Program
{
static void Main(string[] args)
{
while (true)
{
DownloadString("http://www.xxx.asp");
Thread.Sleep(100);//update every 100 millisecoand
}
}
public static void DownloadString(string address)
{
WebClient client = new WebClient();
string website = client.DownloadString(address);
get_Current_X1_value(website);
}
static void get_Current_X1_value(string web)
{
int x = web.IndexOf("Current X1 value:");
string part1 = web.Substring(x, 100);
string[] array = part1.Split('>', '<');
for (int i = 0; i < array.Length; i++)
{
if (array[i].Contains("Current X1 value:"))
Console.Write(array[i]);
if (array[i].Contains("W"))
Console.WriteLine(array[i]);
}
}
}
}
Actually as it is complicated to mix C# and C++ on unix, I am trying to convert C# to C++ so, any help will be very appreciated
Thanking you in advance for your reply, help and time ,
回答1:
Actually as it is complicated to mix C# and C++ on unix, I am trying to convert C# to C++
Have you considered Mono? It is something that's definitely worth checking before starting to learn C++ in order convert and run an existing .NET application on Unix. It's also binary compatible meaning that you don't even need to recompile your existing assembly.
回答2:
Learn C#, learn C++, and spend a lot of time rewriting.
Or use PInvoke from the C# assembly to call into a C++ dll.
Or write managed C++ and compile with the /clr switch. The resulting assembly can be referenced and used from C# projects.
回答3:
It is nearly impossible to directly translate C# to C++ so that it will run on Unix machines.
This is mainly due to the fact that the .NET Framework is not available (from C++) on Unix machines. Mono will allow you to run many C#/.NET programs, but does not support C++/CLI (the C++ extensions that allow directly working with the .NET Framework).
Converting the language is possible - though difficult due to differences in approach (e.g., garbage collection in C#), but the framework calls will require porting to different libraries, and it is often not a good candidate for a direct translation.
For example, in your code above, you'd have to decide on a C++ library for web access - and once you had that choice made, it would dictate the code required to call into that library to download the website string.
回答4:
I'm using C# to C++ converter time to time. It's really great for snippet conversion from c# to c++ or c++/cli.
回答5:
Consider looking at Vala. Vala is a C#-like language that converts into C and then into an executable. There are very little differences with C#. You will still have to use your brain though.
回答6:
You may want to consider CoreRT. It's a .NET project whose goal is to eliminate the need for the CLR to be present on the target platform for running an application. Instead, it generates C++ code from a given C# code. That C++ code is compiled and linked on any target platform that supports C++.
A post on a Microsoft blog said: "If I really want to write some C# code and have it 'just work' on a new IoT device, I don’t have any options until the RyuJIT is capable of generating machine code that works with that processor and operating system." By cross-compiling C# to C++, .Net developers can then deliver their applications without needing to wait for .Net to be deployed on a given platform.
https://github.com/dotnet/corert
回答7:
Edit:
The site listed has been discontinued. I'll leave the old answer here for reference ...
Old answer:
Here is an online converter that will automate the process for you! ...
Varycode online converter
It can do C# to C++ and back again as well as converters for Ruby, Python, Java & VB, apparently!
Edit:
It appears to have had its C++ (and java) functionality removed - it says temporarily, but has done so for a long time now. Hopefully they'll resurrect it soon!
Still works for some other languages (VB, Ruby, Python, Boo).
回答8:
Here is the website where you can find C# to C++ converter.
https://github.com/ASDAlexander77/cs2cpp
You just need to perform some steps to generate and compile C++ code from C#. Additionally this application converts C# code into C++ which is cross platform compatible.
回答9:
As already mentioned here, the translation of libraries can be an issue, but one open source project that might help at some cases is:
http://alexalbala.github.io/Alter-Native/
Citation from its main page:
It provides a tool to easy port applications from high-level languages such as .NET to native languages like C++. It is a research project and it is under development with the collaboration of UPC - BarcelonaTech and AlterAid S.L.
回答10:
static void Main(string[] args)
{
int t = int.Parse(Console.ReadLine());
for (int i = 0; i < t; i++)
{
string []a = Console.ReadLine().Split();
string[] b = Console.ReadLine().Split();
int n = int.Parse(a[0]);
int k = int.Parse(a[1]);
int c = int.MinValue;
for (int l = 0; k<=n; l++)
{
for (int j = l; j < k; j++)
{
if (int.Parse(b[j]) > c)
c = int.Parse(b[j]);
}
k++;
Console.Write(c);
c = int.MinValue;
}
}
来源:https://stackoverflow.com/questions/4651249/how-to-convert-c-sharp-to-c