如何在C#中将浮点数转换为Base36,方法与JavaScript';s.toString(36)做到了

本文关键字:toString JavaScript 浮点数 转换 方法 Base36 | 更新日期: 2023-09-27 18:22:05

我正在尝试将一些代码转换为C#(来自JavaScript),我需要通过C#将一个双数(0.04036483168558814)转换为".toString(36)/Base36"。

此处的JavaScript代码:

 var num = 0.04036483168558814;
 var n = num.toString(36);

输出(n)如下:

0.1gb9f0lx08ij9wwfwkyk5d0a4i

我需要C#的上述结果,那么我将如何在C#中得到同样的结果??

我应用了一些代码,但它们不起作用。。我下面的代码(通过C#):

1)

string OutputVal = Convert.ToString(Int64.Parse("0.04036483168558814"), 36);

string OutputVal = Convert.ToString(Int64.Parse("0.04036483168558814".Substring(2)), 36);

2)

private const string CharList = "0123456789abcdefghijklmnopqrstuvwxyz";
        public static String Encode(long input)
        {
            if (input < 0) throw new ArgumentOutOfRangeException("input", input, "input cannot be negative");
            char[] clistarr = CharList.ToCharArray();
            var result = new Stack<char>();
            while (input != 0)
            {
                result.Push(clistarr[input % 36]);
                input /= 36;
            }
            return new string(result.ToArray());
        }
string OutputString = Encode(Int64.Parse("0.04036483168558814"));
or
string OutputString = Encode(Int64.Parse("0.04036483168558814".Substring(2)));

如何在C#中将浮点数转换为Base36,方法与JavaScript';s.toString(36)做到了

根据MSDN,您可以使用Convert.ToString方法将整数转换为以2、8、10或16为基数的字符串表示形式

如果你想将数字转换为基数36,你可以使用现有的第三方库:

请查看此CodeProject文章或此GitHub项目。

或者您可以编写自己的转换器
例如,此类将整数转换为基数36:

public static class Base36Converter
{
    private const int Base = 36;
    private const string Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static string ConvertTo(int value)
    {
        string result = "";
        while (value > 0)
        {
            result = Chars[value % Base] + result; // use StringBuilder for better performance
            value /= Base;
        }
        return result;
    }
}

请注意,结果将按照big-endian的顺序
例如,十进制的1296将是36进制的100

这里有一个递归函数:

using System;
class Program {
   static string encode(int nIn, int nBase) {
      int n = nIn / nBase;
      char c = "0123456789abcdefghijklmnopqrstuvwxyz"[nIn % nBase];
      return n > 0 ? encode(n, nBase) + c : c.ToString();
   }
   static void Main() {
      var n = 1577858399;
      var s = encode(n, 36);
      Console.WriteLine(s == "q3ezbz");
   }
}

我写了一些代码来获得这个结果。

这是代码:

//==========================================
string Results = Encode(0.04036483168558814);
//==========================================

        private string Encode(double input)
        {
            var result = new Stack<char>();
            char[] clistarr = "0123456789abcdefghijklmnopqrstuvwxyz".ToCharArray();
            Int64 inputValInt = 0;
            string inputValP = "";
            double inputValN = 0;
            string ReturnsVal = "";
            if (input.ToString() != "")
            {
                try
                {
                    if (input.ToString().Contains("."))
                    {
                        inputValP = input.ToString().Substring(0, input.ToString().IndexOf("."));
                        inputValInt = Int64.Parse(inputValP);
                        if (inputValInt != 0)
                        {
                            while (inputValInt != 0)
                            {
                                result.Push(clistarr[inputValInt % 36]);
                                inputValInt /= 36;
                            }
                            char[] RevArr1 = result.ToArray();
                            Array.Reverse(RevArr1);
                            result = new Stack<char>();
                            for (int i = (RevArr1.Length - 1); i >= 0; i--)
                            {
                                result.Push(RevArr1[i]);
                            }
                            result.Push('.');
                        }
                        else
                        {
                            result.Push('0');
                            result.Push('.');
                        }
                        inputValN = input - inputValInt;
                        double inputVal = inputValN;
                        int CountLoop = 0;
                        while (CountLoop < 11)
                        {
                            double tempVal = (inputVal * 36);
                            int Start = tempVal.ToString("R").IndexOf(".");
                            if (Start > 0)
                            {
                                inputValP = tempVal.ToString("R").Substring(0, Start);
                                int TopVal = Int16.Parse(inputValP);
                                result.Push(clistarr[TopVal]);
                                inputVal = tempVal - TopVal;
                            }
                            CountLoop++;
                        }
                        char[] RevArr = result.ToArray();
                        Array.Reverse(RevArr);
                        ReturnsVal = new string(RevArr);
                    }
                    else
                    {
                        inputValInt = Convert.ToInt64(input);
                        while (inputValInt != 0)
                        {
                            result.Push(clistarr[inputValInt % 36]);
                            inputValInt /= 36;
                        }
                        char[] RevArr = result.ToArray();
                        ReturnsVal = new string(RevArr);
                    }
                }
                catch (Exception ee)
                {
                    return ("");
                }
            }
            else
            {
                return ("");
            }
            return (ReturnsVal);
        }

多亏了user2032344的回答,我终于在C++/Qt 中获得了与Javascript Math.random().toString(36)相同的结果

static QString JSMathRandomToString36(double input)
{
    static const auto clistarr = QByteArray("0123456789abcdefghijklmnopqrstuvwxyz");
    QString result;
    for(auto i = 0; i < 11 ; i++){
        auto tempVal = (input * 36);
        auto tempValStr = QString::number(tempVal, 'R');
        auto decIndex = tempValStr.indexOf(".");
        if (decIndex > 0) {
            auto topVal = tempValStr.mid(0, decIndex).toShort();
            result.append(clistarr[topVal]);
            input = tempVal - topVal;
        }
    }
    return (result);
}