数字转换字符,并缩短长度

wmycom 2013-11-01 09:12:01
000000000000 -- 999999999999

12位数字,从12个0到12个9,即一千亿个数字,想做一下缩位编译,如转换成 1h2dHkL3(对应的是123456789012), 要求转换后的 短位编码永不重复
...全文
829 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2013-11-04
  • 打赏
  • 举报
回复 1
连个进制转换自己都写不出来,还跑来写程序,真是让人无语。
threenewbee 2013-11-04
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static string Base62Encode(long num)
        {
            string s = "";
            char[] metachar = Enumerable.Range(0, 10).Select(x => (char)('0' + x))
                .Concat(Enumerable.Range(0, 26).Select(x => (char)('a' + x)))
                .Concat(Enumerable.Range(0, 26).Select(x => (char)('A' + x)))
                .ToArray();
            while (num != 0)
            {
                s = metachar[num % 62].ToString() + s;
                num = num / 62;
            }
            return s;
        }

        static long Base62Decode(string s)
        {
            var dict = Enumerable.Range(0, 10).Select(x => (char)('0' + x))
                .Concat(Enumerable.Range(0, 26).Select(x => (char)('a' + x)))
                .Concat(Enumerable.Range(0, 26).Select(x => (char)('A' + x)))
                .Select((x, i) => new { x, i }).ToDictionary(x => x.x, x => x.i);
            long l = 0;
            for (int i = 0; i < s.Length; i++)
            {
                l = l + (long)dict[s[i]] * (long)Math.Pow((double)62, (double)(s.Length - i - 1));
            }
            return l;
        }

        static void Main(string[] args)
        {
            long l = 999999999999;
            string s = Base62Encode(l);
            Console.WriteLine(s);
            Console.WriteLine(Base62Decode(s));
        }
    }
}
hBxM5A3 999999999999 Press any key to continue . . .
c02645 2013-11-04
  • 打赏
  • 举报
回复
   public string Ten2Hex(string ten)
        {
            ulong tenValue = Convert.ToUInt64(ten);
            ulong divValue, resValue;
            string hex = "";
            do
            {
                divValue = (ulong)Math.Floor(Convert.ToDouble(tenValue / 62));
                resValue = tenValue % 62;
                hex = tenValue2Char(resValue) + hex;
                tenValue = divValue;
            }
            while (tenValue >= 62);
            if (tenValue != 0)
                hex = tenValue2Char(tenValue) + hex;
            return hex;
        }

        public string tenValue2Char(ulong ten)
        {
            switch (ten)
            {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                    return ten.ToString();
                case 10: return "a";
                case 11: return "b";
                case 12: return "c";
                case 13: return "d";
                case 14: return "e";
                case 15: return "f";
                case 16: return "g";
                case 17: return "h";
                case 18: return "i";
                case 19: return "j";
                case 20: return "k";
                case 21: return "l";
                case 22: return "m";
                case 23: return "n";
                case 24: return "o";
                case 25: return "p";
                case 26: return "q";
                case 27: return "r";
                case 28: return "s";
                case 29: return "t";
                case 30: return "u";
                case 31: return "v";
                case 32: return "w";
                case 33: return "s";
                case 34: return "y";
                case 35: return "z";
                case 36: return "A";
                case 37: return "B";
                case 38: return "C";
                case 39: return "D";
                case 40: return "E";
                case 41: return "F";
                case 42: return "G";
                case 43: return "H";
                case 44: return "I";
                case 45: return "J";
                case 46: return "K";
                case 47: return "L";
                case 48: return "M";
                case 49: return "N";
                case 50: return "O";
                case 51: return "P";
                case 52: return "Q";
                case 53: return "R";
                case 54: return "S";
                case 55: return "T";
                case 56: return "U";
                case 57: return "V";
                case 58: return "W";
                case 59: return "S";
                case 60: return "Y";
                case 61: return "Z";
                default: return "";
            }
        }
MessageBox.Show(Ten2Hex("999999999999")); 结果:hBsM5A3
rtdb 2013-11-04
  • 打赏
  • 举报
回复
引用 11 楼 wmycom 的回复:
[quote=引用 8 楼 rtdb 的回复:] 直接用base64编码就是了
用了base64编码,测试结果,输入12位数字,转换成了15位编码,更长了。我要缩短啊[/quote] 实在不知说什么好了,你看7楼都帮你写好了: string s1 = L2S(999999999999); // 6NSlD/8 long l1 = S2L(s1); // 999999999999 12位数字转成了7位编码。
threenewbee 2013-11-04
  • 打赏
  • 举报
回复
引用 10 楼 wmycom 的回复:
[quote=引用 6 楼 caozhy 的回复:] [quote=引用 5 楼 wmycom 的回复:] [quote=引用 3 楼 caozhy 的回复:] 可行,只要目标编码的取值范围大于原来的编码就可以。 比如000000000000 -- 999999999999是1000亿 那么如果你用1-9 a-z A-Z编码,可以缩短到7个字符长度。 最简单的是使用base62编码。也就是将十进制数字转换成62进制数字,对于每一位,如果是0,就表示为0,1表示为1,9表示为9,10表示为a,...35表示为z,36表示为A,...61表示为Z。 你可以参考标准的base64编码,得到你的base62编码。
这样的话。我也想过。如果是还是要解决0-99的对应问题,我做出来过,有重复的[/quote] 这样做肯定可以,有问题也是你代码的问题。[/quote] 用了base 64 编码,输入12位数字,出来15位编码,更长了。。。[/quote] 我能说你什么好呢。
wmycom 2013-11-04
  • 打赏
  • 举报
回复
引用 8 楼 rtdb 的回复:
直接用base64编码就是了
用了base64编码,测试结果,输入12位数字,转换成了15位编码,更长了。我要缩短啊
wmycom 2013-11-04
  • 打赏
  • 举报
回复
引用 6 楼 caozhy 的回复:
[quote=引用 5 楼 wmycom 的回复:] [quote=引用 3 楼 caozhy 的回复:] 可行,只要目标编码的取值范围大于原来的编码就可以。 比如000000000000 -- 999999999999是1000亿 那么如果你用1-9 a-z A-Z编码,可以缩短到7个字符长度。 最简单的是使用base62编码。也就是将十进制数字转换成62进制数字,对于每一位,如果是0,就表示为0,1表示为1,9表示为9,10表示为a,...35表示为z,36表示为A,...61表示为Z。 你可以参考标准的base64编码,得到你的base62编码。
这样的话。我也想过。如果是还是要解决0-99的对应问题,我做出来过,有重复的[/quote] 这样做肯定可以,有问题也是你代码的问题。[/quote] 用了base 64 编码,输入12位数字,出来15位编码,更长了。。。
公西雒 2013-11-01
  • 打赏
  • 举报
回复
引用 2 楼 wmycom 的回复:
[quote=引用 1 楼 danding_ge 的回复:] 这是?可逆加密?
不算加密吧? [/quote]跟加密没啥区别吧
threenewbee 2013-11-01
  • 打赏
  • 举报
回复
可行,只要目标编码的取值范围大于原来的编码就可以。 比如000000000000 -- 999999999999是1000亿 那么如果你用1-9 a-z A-Z编码,可以缩短到7个字符长度。 最简单的是使用base62编码。也就是将十进制数字转换成62进制数字,对于每一位,如果是0,就表示为0,1表示为1,9表示为9,10表示为a,...35表示为z,36表示为A,...61表示为Z。 你可以参考标准的base64编码,得到你的base62编码。
wmycom 2013-11-01
  • 打赏
  • 举报
回复
引用 1 楼 danding_ge 的回复:
这是?可逆加密?
不算加密吧?
公西雒 2013-11-01
  • 打赏
  • 举报
回复
这是?可逆加密?
Michael_wlb1984 2013-11-01
  • 打赏
  • 举报
回复
引用 5 楼 wmycom 的回复:
[quote=引用 3 楼 caozhy 的回复:] 可行,只要目标编码的取值范围大于原来的编码就可以。 比如000000000000 -- 999999999999是1000亿 那么如果你用1-9 a-z A-Z编码,可以缩短到7个字符长度。 最简单的是使用base62编码。也就是将十进制数字转换成62进制数字,对于每一位,如果是0,就表示为0,1表示为1,9表示为9,10表示为a,...35表示为z,36表示为A,...61表示为Z。 你可以参考标准的base64编码,得到你的base62编码。
这样的话。我也想过。如果是还是要解决0-99的对应问题,我做出来过,有重复的[/quote] 保证转换后的值都是7位,不够的前面补零,就不会重复
rtdb 2013-11-01
  • 打赏
  • 举报
回复
直接用base64编码就是了
gomoku 2013-11-01
  • 打赏
  • 举报
回复
static string L2S(long l)
{
    byte[] bytes = BitConverter.GetBytes(l).Reverse().SkipWhile(b => b == 0).ToArray();
    return Convert.ToBase64String(bytes).Replace("=","");
}
static long S2L(string s)
{
    s = s.PadRight((s.Length + 3) / 4 * 4, '=');
    byte[] bytes = Convert.FromBase64String(s).Reverse().Concat(new byte[8]).Take(8).ToArray();
    return BitConverter.ToInt64(bytes, 0);
}
static void Main(string[] args)
{
    string s1 = L2S(999999999999);  // 6NSlD/8
    long l1 = S2L(s1);              // 999999999999

    string s2 = L2S(123);           // ew
    long l2 = S2L(s2);              // 123
}
threenewbee 2013-11-01
  • 打赏
  • 举报
回复
引用 5 楼 wmycom 的回复:
[quote=引用 3 楼 caozhy 的回复:] 可行,只要目标编码的取值范围大于原来的编码就可以。 比如000000000000 -- 999999999999是1000亿 那么如果你用1-9 a-z A-Z编码,可以缩短到7个字符长度。 最简单的是使用base62编码。也就是将十进制数字转换成62进制数字,对于每一位,如果是0,就表示为0,1表示为1,9表示为9,10表示为a,...35表示为z,36表示为A,...61表示为Z。 你可以参考标准的base64编码,得到你的base62编码。
这样的话。我也想过。如果是还是要解决0-99的对应问题,我做出来过,有重复的[/quote] 这样做肯定可以,有问题也是你代码的问题。
wmycom 2013-11-01
  • 打赏
  • 举报
回复
引用 3 楼 caozhy 的回复:
可行,只要目标编码的取值范围大于原来的编码就可以。 比如000000000000 -- 999999999999是1000亿 那么如果你用1-9 a-z A-Z编码,可以缩短到7个字符长度。 最简单的是使用base62编码。也就是将十进制数字转换成62进制数字,对于每一位,如果是0,就表示为0,1表示为1,9表示为9,10表示为a,...35表示为z,36表示为A,...61表示为Z。 你可以参考标准的base64编码,得到你的base62编码。
这样的话。我也想过。如果是还是要解决0-99的对应问题,我做出来过,有重复的

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧