解决方案 »
- 请教几个socket的问题
- DllImport 调用 dll文件错误,引用了一个外部dll 就是说找不到文件!!winmm.dll 关于多媒体定时器的
- 求一个Remoting,访问数据库且使用SqlHelper的示例
- 应用软件数据库 高手们进来
- C#网页控件webbrower中的相对路径怎么表示?
- c#调用c++的dll遇到的问题_StringBuilder
- 如何用程序自动调用MyIE打开指定地址?顶者有分
- 数据显示问题,美女请进。。。还有帅哥!
- 我这个哪里出了问题啊?数据库中无法添加呢~~
- 用剩下的所有分所一个用C#操作DHTML EDITING的例子!
- 求助,Activex 无法调用到动态链接库DLL
- 求助:把一个按钮事件里面的方法提取出来,做成公用方法,以便其他事件可以调用
那么如果你用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编码。
{
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
}
用了base 64 编码,输入12位数字,出来15位编码,更长了
用了base64编码,测试结果,输入12位数字,转换成了15位编码,更长了。我要缩短啊
用了base 64 编码,输入12位数字,出来15位编码,更长了我能说你什么好呢。
用了base64编码,测试结果,输入12位数字,转换成了15位编码,更长了。我要缩短啊实在不知说什么好了,你看7楼都帮你写好了:
string s1 = L2S(999999999999); // 6NSlD/8
long l1 = S2L(s1); // 99999999999912位数字转成了7位编码。
{
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
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 . . .