Base36 [英] Base36

查看:72
本文介绍了Base36的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都有c#Base10ToBase36和Base36ToBase10转换例程? TIA


-

William Stacey,MVP
http://mvp.support.microsoft.com

Anyone have a c# Base10ToBase36 and Base36ToBase10 conversion routines? TIA

--
William Stacey, MVP
http://mvp.support.microsoft.com

推荐答案

William ,


这是我前段时间做的事情 - 实际上是针对不同的基础,

但是很容易改变以处理base32。


你没有为你的数字基础指定符号集 - 我将假设

0,1,2,3 ... X,Y,Z。如果你的不同,请相应更改标记字符串




出于性能原因,数字的权重是在编译时计算的

time。


note - 绝对没有错误检查,它只处理正值
值,并假设所有字符代码都是大写。


问候

roy罚款

名称空间CONVERSION {

//处理只有正数的值4,738,381,338,321,616,896 - 1;

公共类BASE32 {

static string tokens =" 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;;

static long [] powers =

{1L,36L,36L * 36L,36L * 36L * 36L,36L * 36L * 36L * 36L,36L * 36L * 36L * 36L * 36L,36L * 36L * 36L *

36L * 36L * 36L,36L * 36L * 36L * 36L * 36L * 36L * 36L,36L * 36L * 36 L * 36L * 36L * 36L * 36L * 36L,36L *

36L * 36L * 36L * 36L * 36L * 36L * 36L * 36L,36L * 36L * 36L * 36L * 36 L * 36L * 36L * 36L * 36L * 36L,36L *

36L * 36L * 36L * 36L * 36L * 36L * 36L * 36L * 36L * 36L};


pu blic static string ToString(long lval){

int maxStrLen = powers.Length;

long curval = lval;

char [] tb = new char [maxStrLen];

int outpos = 0;

for(int i = 0; I< maxStrLen; i ++){

long pval = powers [maxStrLen - i - 1];

int pos =(int)(curval / pval);

tb [outpos ++] = tokens.Substring(pos,1).ToCharArray()[0];

curval = curval%pval;

}

if(outpos == 0)tb [outpos ++] =''0'';

返回新字符串(tb,0,outpos).TrimStart(''0''); < br $>
}


public static long ToLong(string t){

long ival = 0;

char [] tb = t.ToCharArray();

for(int i = 0; i< tb.Length; i ++){

ival + = powers [i] * tokens.IndexOf(tb [tb.Length-i-1]);

}

返回ival;

}

}

}


" William Stacey [MVP]" < ST *********** @ mvps.org>在消息中写道

新闻:uc **************** @ TK2MSFTNGP12.phx.gbl ...
William,

this is something that i did some time ago - actually for a different base,
but it was easy enough to change to handle base32.

you did not specify the symbol set for your number base - i will assume
0,1,2,3... X,Y,Z. if yours is different, change the tokens string
accordingly.

for performance reasons, the weights of the digits are computed at compile
time.

note - there is absolutely no error checking, and it handles only positive
values, and assumes that all character codes are upper case.

regards
roy fine
namespace CONVERSION{
// handles positive only values up to 4,738,381,338,321,616,896 - 1;
public class BASE32{
static string tokens = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static long [] powers =
{1L,36L,36L*36L,36L*36L*36L,36L*36L*36L*36L,36L*36 L*36L*36L*36L,36L*36L*36L*
36L*36L*36L,36L*36L*36L*36L*36L*36L*36L,36L*36L*36 L*36L*36L*36L*36L*36L,36L*
36L*36L*36L*36L*36L*36L*36L*36L,36L*36L*36L*36L*36 L*36L*36L*36L*36L*36L,36L*
36L*36L*36L*36L*36L*36L*36L*36L*36L*36L};

public static string ToString(long lval){
int maxStrLen = powers.Length;
long curval = lval;
char [] tb = new char[maxStrLen];
int outpos = 0;
for(int i=0; i<maxStrLen; i++){
long pval = powers[maxStrLen - i - 1];
int pos = (int)(curval / pval);
tb[outpos++] = tokens.Substring(pos,1).ToCharArray()[0];
curval = curval % pval;
}
if(outpos==0) tb[outpos++] = ''0'';
return new string(tb,0,outpos).TrimStart(''0'');
}

public static long ToLong(string t){
long ival = 0;
char [] tb = t.ToCharArray();
for(int i=0; i<tb.Length; i++){
ival += powers[i]*tokens.IndexOf(tb[tb.Length-i-1]);
}
return ival;
}
}
}

"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:uc****************@TK2MSFTNGP12.phx.gbl...
任何人都有交流#Base10ToBase36和Base36ToBase10转换例程?
TIA
-
William Stacey,MVP
http://mvp.support.microsoft.com
Anyone have a c# Base10ToBase36 and Base36ToBase10 conversion routines? TIA
--
William Stacey, MVP
http://mvp.support.microsoft.com



嘿,非常感谢Roy。还要发布其他基地吗?无论哪种方式,

再次感谢!!


-

William Stacey,MVP
http://mvp.support.microsoft.com


Roy Fine < RL **** @ twt.obfuscate.net>在消息中写道

新闻:#w ************** @ TK2MSFTNGP09.phx.gbl ...
Hey thanks a lot Roy. Care to post the other base as well? Either way,
thanks again!!

--
William Stacey, MVP
http://mvp.support.microsoft.com

"Roy Fine" <rl****@twt.obfuscate.net> wrote in message
news:#w**************@TK2MSFTNGP09.phx.gbl...
William,

这是我前段时间做过的事情 - 实际上对于不同的
基础,但很容易改变以处理base32。

你没有指定符号设置为您的数字基础 - 我将假设
0,1,2,3 ... X,Y,Z。如果你的不同,请相应地更改标记字符串。

出于性能原因,数字的权重是在编译时计算的。

注意 - 绝对没有错误检查,它只处理正值
,并假设所有字符代码都是大写的。

问候
roy fine

名称空间CONVERSION {
//仅处理正数值4,738,381,338,321,616,896 - 1;
公共类BASE32 {
static string tokens =" 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;;
static long [ ] powers =

{1L,36L,36L * 36L,36L * 36L * 36L,36L * 36L * 36L * 36L,36L * 36 L * 36L * 36L * 36L,36L * 36L * 36L *
36L * 36L * 36L,36L * 36L * 36L * 36L * 36L * 36L * 36L,36L * 36L * 36 L * 36L * 36L * 36L * 36L * 36L,36L *
36L * 36L * 36L * 36L * 36L * 36L * 36L * 36L,36L * 36L * 36L * 36L * 36 L * 36L * 36L * 36L * 36L * 36L,36L * 36L * 36L * 36L * 36L * 36L * 36L * 36L * 36L * 36L * 36L};

公共静态字符串ToString(long lval){
int maxStrLen = powers.Length;
l ong curval = lval;
char [] tb = new char [maxStrLen];
int outpos = 0;
for(int i = 0; I< maxStrLen; i ++){
long pval = powers [maxStrLen - i - 1];
int pos =(int)(curval / pval);
tb [outpos ++] = tokens.Substring(pos, 1).ToCharArray()[0];
如果(outpos == 0)tb [outpos ++] =''0''; >返回新字符串(tb,0,outpos).TrimStart(''0'');
}
公共静态长ToLong(字符串t){
long ival = 0;
char [] tb = t.ToCharArray();
for(int i = 0; i< tb.Length; i ++){
ival + = powers [i] * tokens .IndexOf(tb [tb.Length-i-1]);
}
返回ival;
}
}
}

William Stacey [MVP]" < ST *********** @ mvps.org>在消息中写道
新闻:uc **************** @ TK2MSFTNGP12.phx.gbl ...
William,

this is something that i did some time ago - actually for a different base, but it was easy enough to change to handle base32.

you did not specify the symbol set for your number base - i will assume
0,1,2,3... X,Y,Z. if yours is different, change the tokens string
accordingly.

for performance reasons, the weights of the digits are computed at compile
time.

note - there is absolutely no error checking, and it handles only positive
values, and assumes that all character codes are upper case.

regards
roy fine
namespace CONVERSION{
// handles positive only values up to 4,738,381,338,321,616,896 - 1;
public class BASE32{
static string tokens = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static long [] powers =
{1L,36L,36L*36L,36L*36L*36L,36L*36L*36L*36L,36L*36 L*36L*36L*36L,36L*36L*36L* 36L*36L*36L,36L*36L*36L*36L*36L*36L*36L,36L*36L*36 L*36L*36L*36L*36L*36L,36L* 36L*36L*36L*36L*36L*36L*36L*36L,36L*36L*36L*36L*36 L*36L*36L*36L*36L*36L,36L* 36L*36L*36L*36L*36L*36L*36L*36L*36L*36L};

public static string ToString(long lval){
int maxStrLen = powers.Length;
long curval = lval;
char [] tb = new char[maxStrLen];
int outpos = 0;
for(int i=0; i<maxStrLen; i++){
long pval = powers[maxStrLen - i - 1];
int pos = (int)(curval / pval);
tb[outpos++] = tokens.Substring(pos,1).ToCharArray()[0];
curval = curval % pval;
}
if(outpos==0) tb[outpos++] = ''0'';
return new string(tb,0,outpos).TrimStart(''0'');
}

public static long ToLong(string t){
long ival = 0;
char [] tb = t.ToCharArray();
for(int i=0; i<tb.Length; i++){
ival += powers[i]*tokens.IndexOf(tb[tb.Length-i-1]);
}
return ival;
}
}
}

"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:uc****************@TK2MSFTNGP12.phx.gbl...
任何人都有ac#Base10ToBase36和Base36ToBase10转换例程?
Anyone have a c# Base10ToBase36 and Base36ToBase10 conversion routines?


TIA


TIA


-
William Stacey,MVP
http://mvp.support.microsoft.com




可以通过字母表来应用通用基本转换:


private void char [] alphabet = new char [] {'' 0 '', '' 1 '', '' 2 '', '' 3 '', '' 4 ''}; //基础5

私有字符串IntegerToBase(int foo,char []字母表){

int base = alphabet.Length;

string baseStr ="" ;;

do {

baseStr = alphabet [foo%base] + baseStr;

foo / =基础;

}而(foo> 0);


返回baseStr

}


给定任何字母表,你现在可以从整数到基数。

反过来更复杂,但只是因为某些假设

必须是制作(从具有特定规则的众所周知的类型转换

如整数总是比从任意类型转换更容易

其中规则不是自我强加的一个字符串)。


我相信你也需要反过来。我已经在博客空间中进行了大量的解析

和转换例程,所以我会尝试将这个提升为博客文章,然后将其提交给博客。包含链接的文章

以及我所有解析例程的简短细节,以使它们更加可用

可访问。


-

Justin Rogers

DigiTec Web Consultants,LLC。

博客: http://weblogs.asp.net/justin_rogers


" William Stacey [MVP]" < ST *********** @ mvps.org>在消息中写道

news:u
A generic base conversion can be applied by taking an alphabet:

private void char[] alphabet = new char[] {''0'',''1'',''2'',''3'',''4''}; // Base 5

private string IntegerToBase(int foo, char[] alphabet) {
int base = alphabet.Length;
string baseStr = "";
do {
baseStr = alphabet[foo%base] + baseStr;
foo /= base;
} while(foo > 0);

return baseStr
}

Given any alphabet, you can now go one way from integers to the base.
The reverse is more complicated, but only because certain assumptions
have to be made (converting from a well known type with specific rules
such as an integer is always easier than converting from an arbitrary type
where rules aren''t self-imposed within a string).

I''m sure you also need the reverse. I already have numerous parsing
and conversion routines located in the blog space, so I''ll try and elevate
this to a blog posting, and then back it with an article that contains links
and short details to all of my parsing routines to make them more
accessible.

--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:u


这篇关于Base36的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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