C#缩短到INT区分大小写code [英] c# Shorten int into case sensitive code

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

问题描述

有字母表中26个字符(abc..yz)和10位(0..9)。这使我们的62个字符的词典,如果我们去区分大小写使用。

目前,我们正在建立一个基于查询的ID文件名的一部分。这些数字可以得到相当长,所以我们想缩短他们。例如,而不是具有:

  file_459123.exe

我们宁愿:

  file_aB5.exe

有没有人有在C#中的方法,可以一个int转换成一个较短的字符串,区分大小写,并转换成字符串,区分大小写回一个int?

实施例(不必须是这种模式):

  1 = 1
2 =
...
9 = 9
10 =一
11 = B
...
36 = Z
37 = A


解决方案

只是扩大M4Ns解决了泛型类....

 公共类BaseX
    {
        私人只读字符串_digits;        公共BaseX(串位)
        {
            _digits =数字;
        }
        公共字符串ToBaseX(INT数)
        {
            VAR输出=;
            做
            {
                输出= _digits [编号%_digits.Length] +输出;
                数=号/ _digits.Length;
            }
            而(数大于0);
            返回输出;
        }        公众诠释FromBaseX(串号)
        {
            返回number.Aggregate(0,(A,C)=>一种* _digits.Length + _digits.IndexOf(c)项);
        }
    }

然后你可以做...

 变种x =新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斌=新BaseX(01);
            Console.WriteLine(bin.ToBaseX(10));

There are 26 characters in the alphabet (abc..yz) and 10 digits (0..9). That gives us a lexicon of 62 characters to use if we go case sensitive.

At the moment we are building a part of a filename based on an ID in our database. 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?

Example (doesn't have to be this pattern):

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

解决方案

just expanding M4Ns solution to a generic class....

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

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

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