简体   繁体   English

c#将int缩短为区分大小写的代码

[英]c# Shorten int into case sensitive code

There are 26 characters in the alphabet (abc..yz) and 10 digits (0..9). 字母表中有26个字符(abc..yz)和10个数字(0..9)。 That gives us a lexicon of 62 characters to use if we go case sensitive. 如果我们区分大小写,那么我们可以使用62个字符的词典。

At the moment we are building a part of a filename based on an ID in our database. 目前,我们正在根据数据库中的ID构建文件名的一部分。 These numbers can get quite long so we would like to shorten them. 这些数字可能会很长,所以我们想缩短它们。 For example instead of having: 例如,而不是:

file_459123.exe

We would rather: 我们宁愿:

file_aB5.exe

Does anyone have a method in C# that can convert an int into a shorter case sensitive string, and convert a case sensitive string back into an int? 有没有人在C#中有一个方法可以将int转换为更短的区分大小写的字符串,并将区分大小写的字符串转换回int?

Example (doesn't have to be this pattern): 示例(不一定是这种模式):

1 = 1
2 = 2
...
9 = 9
10 = a
11 = b
...
36 = z
37 = A

Despite the Base64 references, here's a generic (non-optimized) solution: 尽管有Base64参考,但这是一个通用(非优化)解决方案:

// for decimal to hexadecimal conversion use this:
//var digits = "0123456789abcdef".ToCharArray();

var digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
             .ToCharArray();

int number = 459123;
string output = "";

do
{
    var digit = digits[(int)(number%digits.Length)];
    output = output.Insert(0, digit.ToString());
    number = (int)number/digits.Length;
}
while (number > 0);

// output is now "1Vrd"

试试Base64

just expanding M4Ns solution to a generic class.... 只是将M4Ns解决方案扩展到通用类....

  public class BaseX
    {
        private readonly string _digits;

        public BaseX(string digits)
        {
            _digits = digits;
        }
        public string ToBaseX(int number)
        {           
            var output = "";
            do
            {                
                output = _digits[number % _digits.Length] + output;
                number = number / _digits.Length;
            }
            while (number > 0);
            return output;
        }

        public int FromBaseX(string number)
        {
            return number.Aggregate(0, (a, c) => a*_digits.Length + _digits.IndexOf(c));
        }
    }

and then you can do... 然后你可以做...

var x = new BaseX("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
            Console.WriteLine(x.ToBaseX(10));
            Console.WriteLine(x.ToBaseX(459123));
            Console.WriteLine(x.ToBaseX(63));

            Console.WriteLine(x.FromBaseX("1Vrd"));
            Console.WriteLine(x.FromBaseX("A"));

            var bin = new BaseX("01");
            Console.WriteLine(bin.ToBaseX(10));

Depending on your situation you might want to look at using Base32 as the restricted character set may be easier to read (IE, some users cannot easily distinguish the difference between zero and the letter o). 根据您的情况,您可能希望查看使用Base32,因为受限制的字符集可能更容易阅读(IE,一些用户无法轻易区分零和字母o之间的差异)。

You can find examples here and here . 你可以在这里这里找到例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM