How to Convert C# to C++ [Closed]

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("");
                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++

7

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.

13

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.

0

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.

2

I'm using C# to C++ converter time to time. It's really great for snippet conversion from c# to c++ or c++/cli.

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.

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.

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).

5

As already mentioned here, the translation of libraries can be an issue, but one open source project that might help at some cases is:

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.

1
Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest