任意進製與10進製的互轉 62進製

2021-09-05 09:05:29 字數 1264 閱讀 9846

假設乙個r進製的數為an-1an-2an-3…a0

將其轉換為10進製的公式為

y = an-1*rn-1 + an-1*rn-2 + … + a0*r0

r0的值為1, 因此也可以寫為

y = an-1*rn-1 + an-1*rn-2 + … + a0 (公式1)

公式1可以變形為

y = a0 + r * w, 其中 w = an-1*rn-2 + … +a1

a0為y/r的餘數

w 為y/r的商

然後對w執行相同的過程, 分別得到a1, a2, … , an-1

以下是10進製與62進製的互轉方式,62進製用0-9a-za-z中的字元表示

public static string radix10to62(long num) 

final int radix = 62;

char outs = new char[64];

int outsindex = outs.length;

long quotient;

long remainder;

char c;

doelse if(remainder >= 10 && remainder <= (10+26-1)) else

outs[--outsindex] = c;

num = quotient;

}while(num>0);

return new string(outs, outsindex, outs.length - outsindex);

}public static long radix62to10(string num) else if(c>='a' && c <='z') else if(c>='a' && c <='z') else

out = out*62 + charnum;

} return out;

}

測試

public static void main(string args) throws exception
列印結果

azl8n0y58m7, 9223372036854775807

進製轉化進10進製數

option explicit private sub form click dim st as integer,dem as long st inputbox 輸入乙個十六進製制數 dem convert st print st dem end sub private function conve...

任意進製轉換10進製和10進製轉換成任意進製

原理 輸入是乙個字串,由函式體轉換成數 考慮是否溢位int範圍 include using namespace std include typedef long long ll 這個具體要看要求,如果給的輸入是否會超過乙個int的範圍 include ll transdec string num,i...

36進製數與10進製數互轉

10進製整數轉化成36進製字串 include stdafx.h include int main for i 0 num i for i i 0 i printf c a i 10 a i 0 a i 10 a printf n return 0 引數描述 hex10 被轉換10進製數字 測試方法...