|
发表于 2012-7-16 09:21:44
|
显示全部楼层
先上一个把十进制数转成任意进制数,这是算法支持十以上进制
[jass]
globals
constant string Weights = "0123456789ABCDEFGHIJKLMNOPQRSTUV"
//Supports only 32 hex
endglobals
function GetHexString takes integer index returns string
return SubString(Weights, index-1, index)
endfunction
function GetStringWeights takes string num returns integer
local integer i = 1
loop
exitwhen i > 32
if SubString(Weights, i - 1, i ) == num then
return i
endif
set i = i + 1
endloop
endfunction
function A2Ds takes integer number,integer hex returns string
local string O = null
local integer temp
if number < 0 then
set number = 0 - number
set O = "-"
endif
loop
exitwhen number == 0
set temp = number - ( number / hex ) * hex
set O = O + GetHexString(temp)
set number = number / hex
endloop
return O
endfunction
function A2D takes integer number,integer hex returns integer
return S2I(A2Ds(number,hex))
endfunction
//超过十进制不要使用A2D函数
把任意进制转成十进制
//不支持带符号的进制数
function sD2A takes string number ,integer hex returns integer
local integer Len = StringLength(number)
local integer dnumber = 0
local integer index = 1
loop
exitwhen index > Len
set dnumber = dnumber + GetStringWeights(SubString(number,index-1,index)) * Pow(hex,Len - index)
set index = index + 1
endloop
return dnumber
endfunction
function D2A takes integer number ,integer hex returns integer
return sD2A(I2S(number),hex)
endfunction[/jass] |
|