Convert C++ code to C# or Java

websss
websss used Ask the Experts™
on
Can some help me convert this C++ code to either java or C#
uint32 SN2ID(const string &szTermSN, string &szTermID)
{
       if(szTermSN.length() == 8 )
       {
              int f1,f2,f3,f4;
              sscanf(szTermSN.c_str(), "%02d%02d%02d%02d", &f1, &f2, &f3, &f4);
              f2 += 128;
              f3 += 128;
              char tmp[10] = "";
              _snprintf(tmp, sizeof(tmp), "%02X%02X%02X%02X", f1, f2, f3, f4);
              szTermID = tmp;
       }
       return 0;
}

Open in new window


if  for example  szTermSN=23D39449 szTermID=35832073
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Rikin ShahMicrosoft Dynamics CRM Consultant

Commented:
Hi,

Check below-

using System;

public class Program
{
    public static void Main(string[] args)
    {
        //Console.ReadLine();
        int f1, f2, f3, f4;
        int.TryParse(args[0], out f1);
        int.TryParse(args[0], out f2);
        int.TryParse(args[2], out f3);
        int.TryParse(args[3], out f4);

        Console.WriteLine(SN2ID("testtest", f1, f2, f3, f4));
        Console.WriteLine("Hello World");
        Console.ReadKey();
    }

    private static string SN2ID(string szTermSN, int f1, int f2, int f3, int f4)
    {
        string tmp = string.Empty;
        Console.WriteLine(szTermSN.Length.ToString());
        if (szTermSN.Length == 8)
        {

            f2 += 128;
            f3 += 128;

            tmp = string.Format("{0}{1}{2}{3}", f1.ToString("X"), f2.ToString("X"), f3.ToString("X"), f4.ToString("X"));
        }

        return tmp;
    }
}

Open in new window

Top Expert 2016

Commented:
Java and C# are very different things. To do it properly in Java, it's necessary to know how it's going to  be called
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Here's an example in C#, should give you a good starting point.

static void Main()
{
    string szIn;
    string szOut;

    szIn = "15990331";

    SN2ID(szIn, out szOut);

    Console.WriteLine(szOut);

    int SN2ID(string szTermSN, out string szTermID)
    {
        try
        {
            // Break up input string in 2 digit pairs, try to convert to integers
            int f1 = int.Parse(szTermSN.Substring(0, 2));
            int f2 = int.Parse(szTermSN.Substring(2, 2));
            int f3 = int.Parse(szTermSN.Substring(4, 2));
            int f4 = int.Parse(szTermSN.Substring(6, 2));

            // Add 128 to middle two values
            f2 += 128;
            f3 += 128;

            // Build returned string of two character hex codes of each value
            szTermID = f1.ToString("X2") + f2.ToString("X2") + f3.ToString("X2") + f4.ToString("X2");
        }
        catch
        {
            // Errors parsing input into integers
            szTermID = "";
            return -1;
        }

        // Successful return here
        return 0;

    }
}

Open in new window


»bp
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Top Expert 2016

Commented:
Not even sure why it's necessary to translate it. You could just leave it as it and call it from C#
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
I think you have you example case backwards,

"if  for example  szTermSN=23D39449 szTermID=35832073"

should be:

"if  for example  szTermSN=35832073 szTermID=23D39449"

which is exactly what my code returns...


»bp
websssCEO

Author

Commented:
Hi CEHJ
How would I reference is as the project is in C# not C ?
Top Expert 2016

Commented:
Well, that's not my field, but i know you can ;) The very worst case is to turn the source into a dll and load it. Someone else can probably comment more on this issue
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
To use in your C# project just include the procedure:

    int SN2ID(string szTermSN, out string szTermID)
    {
        try
        {
            // Break up input string in 2 digit pairs, try to convert to integers
            int f1 = int.Parse(szTermSN.Substring(0, 2));
            int f2 = int.Parse(szTermSN.Substring(2, 2));
            int f3 = int.Parse(szTermSN.Substring(4, 2));
            int f4 = int.Parse(szTermSN.Substring(6, 2));

            // Add 128 to middle two values
            f2 += 128;
            f3 += 128;

            // Build returned string of two character hex codes of each value
            szTermID = f1.ToString("X2") + f2.ToString("X2") + f3.ToString("X2") + f4.ToString("X2");
        }
        catch
        {
            // Errors parsing input into integers
            szTermID = "";
            return -1;
        }

        // Successful return here
        return 0;

    }

Open in new window

And then call as needed with:

    SN2ID(szIn, out szOut);

Open in new window


»bp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial