12

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++

Dharman
  • 30,962
  • 25
  • 85
  • 135
make
  • 755
  • 7
  • 21
  • 37
  • 9
    Translating from one language to another is never easy because of the standard libraries involved. For instance, I might be able to translate your code if you provide me with a C++ implementation of `System.Net.WebClient`. Or did you mean C++/CLI? – Frédéric Hamidi Jan 10 '11 at 20:38
  • 1
    If you have to port that to unix, I would suggest bash+wget+perl rather than C++. – Ben Voigt Jan 10 '11 at 20:40
  • 2
    Could you make a more specific list of the problems you've run into? – FrustratedWithFormsDesigner Jan 10 '11 at 20:41
  • @make, you also might want to modify this code before translating it. Hitting a web server 10 times per second is probably not such a good idea. – Frédéric Hamidi Jan 10 '11 at 20:49
  • C++/CLI is not currently supported via Mono (the syntax extensions do are not implemented yet). If you need it to run on UNIX, just compile the C# code using Mono. If you need a UNIX-only implementation, PERL is the way to go and will save you a lot of headaches. – Zac Howland Jan 10 '11 at 20:52
  • thanks for replies. 1) does C++/CLI is supported ON UNIX? @ Ben Voigt, could you please give me more details on using bash+wget+perl ... thanks to all of you! – make Jan 10 '11 at 21:31
  • thanks to all of you for havinf shared your ideas with me ... – make Jan 10 '11 at 21:42

8 Answers8

25

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I believe that one of the main reasons for the inception of the Mono project was to make it very easy to mix C++ code with .NET code, especially C#. – Cheeso Jan 10 '11 at 20:39
  • 11
    @Cheeso, no one of the main reasons for the inception of the Mono project was to provide a CLR implementation for *nix systems. – Darin Dimitrov Jan 10 '11 at 20:40
  • I was just getting ready to link the mono site ... you beat me to it! – Zac Howland Jan 10 '11 at 20:48
  • Yes! I considered Mono> however it requires time and a lot of work to have it working on Unix ... – make Jan 10 '11 at 20:48
  • 4
    @make, installing Mono requires far less work than porting the code you have posted to C++. – Darin Dimitrov Jan 10 '11 at 20:49
  • In addition, mixing C# and C++ requires to use .dll file on MS Windows . However, dll is not supported on UNIx – make Jan 10 '11 at 20:50
  • @make, why do you want to mix C# with C++? Why don't you use only managed code? – Darin Dimitrov Jan 10 '11 at 20:52
  • @Darin Dimitrov, thanks for replies. is there any pakg for Unix? – make Jan 10 '11 at 21:05
  • @makes, yes there is one for Solaris: http://www.go-mono.com/mono-downloads/download.html. – Darin Dimitrov Jan 10 '11 at 21:06
  • @Darin Dimitrov, could you please give me more details how to use managed code ????? thanks! – make Jan 10 '11 at 21:07
  • @make, what you have posted in your question is managed code. C# is managed code. Everything that gets compiled to IL and runs on the CLR is managed code. Mono helps you run managed code on *nix systems. – Darin Dimitrov Jan 10 '11 at 21:08
  • @Darin Dimitrov, thhanks for sharing your ideas with me ... – make Jan 10 '11 at 21:39
  • Dimitrov, Providing a CLR on *nix was never an end goal of Mono. I didn't state my point clearly. what I meant to say was, the reason Miguel pursued Mono in the early days was to enable mixing languages, like C++ and C#, on Linux. http://mail.gnome.org/archives/gnome-hackers/2002-February/msg00031.html Mono does provide a CLR (more or less) on *nix, but was not conceived *for that purpose*. CLR is a means to an end. The end goal was to be able to mix languages more easily (libraries and applications), and Miguel envisioned the Mono runtime (aka CLR) as the vehicle to provide that capability. – Cheeso Jan 11 '11 at 04:30
11

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.

Trystan Spangler
  • 1,685
  • 11
  • 20
9

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.

Paul Ratazzi
  • 6,289
  • 3
  • 38
  • 50
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • there are plenty of project which can do it. 3rd parties or from Microsoft (for example CoreRT) – Alexander77 Oct 27 '16 at 09:04
  • @Alexander77 CoreRT doesn't do it - it's a separate implementation that wraps native stuff in a COM like API that's usable from C#... – Reed Copsey Oct 27 '16 at 18:34
6

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

igorushi
  • 1,855
  • 21
  • 19
4

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.

Samuel Allan
  • 392
  • 2
  • 20
4

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

zumalifeguard
  • 8,648
  • 5
  • 43
  • 56
3

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

noelicus
  • 14,468
  • 3
  • 92
  • 111
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/16218886) – Rook May 24 '17 at 12:13
  • 2
    The link *is* the answer. Your comment makes no sense. There is nothing else for me to say. I suspect you have not even read the question and considered how this answers it. I've edited the answer to be as descriptive as possible for a simple link-share-that-answers-the-question. – noelicus May 24 '17 at 12:45
  • @noelicus Is there an alternative? A message currently displays with an apology from varycode that C++ conversion is unavailable. – WonderWorker Nov 07 '17 at 11:50
  • Hmm - I don't know, sorry. – noelicus Nov 07 '17 at 12:21
  • 2
    The whole domain is now for sale... – Happypig375 Dec 02 '18 at 11:44
2

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.

Martin Vahi
  • 129
  • 1
  • 5