C#到C [英] C# to C

查看:91
本文介绍了C#到C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

请帮我,我想将C#类转换为C代码

Please help me I want to convert below C# class to C code

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management;
using System.Security.AccessControl;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ZenSetGroupPolicy
{
    public class SNGenerator
    {
        public enum
SNKeyLength
        {
            SN16 = 16, SN20 = 20, SN24 = 24, SN28 = 28, SN32 = 32
        }
        public enum SNKeyNumLength
        {
            SN4 = 4, SN8 = 8, SN12 =
            12
        }
        public static class RandomSNKGenerator
        {
            private static string
            AppendSpecifiedStr(int length, string str, char[] newKey)
            {
                string
                newKeyStr = "";
                int k = 0;
                for (int i = 0; i < length; i++)
                {
                    for
                    (k = i; k < 4 + i; k++)
                    {
                        newKeyStr += newKey[k];
                    }
                    if (k ==
                    length)
                    {
                        break;
                    }
                    else
                    {
                        i = (k) - 1;
                        newKeyStr +=
                        str;
                    }
                }
                return newKeyStr;
            }
            /// 

            /// Generate
            //standard serial key with alphanumaric format
            /// 
            ///
            //the supported length of the serial
            //    key
            /// returns formated serial
            //key
            public static string GetSerialKeyAlphaNumaric(SNKeyLength
            keyLength, string input)
            {
                string newSerialNumber = "";
                 
                using (MD5 md5 = MD5.Create())
                {
                    byte[] hash = md5.ComputeHash(Encoding.Default.GetBytes(input));
                    Guid newguid = new Guid(hash);
                    string randomStr =
                    newguid.ToString("N");
                    string tracStr = randomStr.Substring(0,
                    (int)keyLength);
                    tracStr = tracStr.ToUpper();
                    char[] newKey =
                    tracStr.ToCharArray();
                    switch (keyLength
                    )
                    {
                        case SNKeyLength.SN16:
                            newSerialNumber = AppendSpecifiedStr(16,
                            "-", newKey);
                            break;
                        case SNKeyLength.SN20:
                            newSerialNumber =
                            AppendSpecifiedStr(20, "-", newKey);
                            break;
                        case
                    SNKeyLength.SN24:
                            newSerialNumber = AppendSpecifiedStr(24, "-",
                            newKey);
                            break;
                        case SNKeyLength.SN28:
                            newSerialNumber =
                            AppendSpecifiedStr(28, "-", newKey);
                            break;
                        case
                    SNKeyLength.SN32:
                            newSerialNumber = AppendSpecifiedStr(32, "-",
                            newKey);
                            break;
                    }
                }
                return newSerialNumber;
            }
            ///

            /// Generate serial key with only numaric
            ///

            /// the supported length of
            //the serial key
            /// returns formated serial
            //key
            public static string GetSerialKeyNumaric(SNKeyNumLength
            keyLength)
            {
                Random rn = new Random();
                double sd =
                Math.Round(rn.NextDouble() * Math.Pow(10, (int)keyLength) + 4);
                return
                sd.ToString().Substring(0, (int)keyLength);
            }
        }
        public static void GrantAccess()
        {
          
        }
    }
}

输入:  SNGenerator.RandomSNKGenerator.GetSerialKeyAlphaNumaric(SNGenerator.SNKeyLength.SN20,"Hello World");

Input : SNGenerator.RandomSNKGenerator.GetSerialKeyAlphaNumaric(SNGenerator.SNKeyLength.SN20, "Hello World");

输出:  B18D-0AB1-E064-4175-05B7

Output : B18D-0AB1-E064-4175-05B7

推荐答案

否,你将无法将其翻译成C.让我们认为:并不是说你不能翻译C#代码本身,你可以通过一些努力来完成。问题是该代码中的大部分工作是由MD5类完成的,它是C#可用的Framework类库的
部分。 C中没有这个课程。我不怀疑,如果你环顾四周,你可能会找到一些在C中计算MD5哈希的源代码,但它不再是翻译$的问题。 b $ b代码,但找到  另一个代码。
No, you will not be able to translate this into C. Let's qualify that: it's not that you can't translate the C# code itself, which you could do with some effort. The problem is that the bulk of the work in that code is done by the MD5 class, which is part of the Framework Class Library available to C#. This same class is not available in C. I don't doubt that if you look around you might be able to find some source code for computing an MD5 hash in C, but it is no longer a matter of translating the code, but of finding another piece of code.


这篇关于C#到C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆