|
发表于 2006-4-12 18:39:23
|
显示全部楼层
字符串可以直接连接的,这样效率高一点
以下是我写的任意进制转换(2~36)
string彻底研究V1.17。现在终于可以方便的按字符处理字符串了
http://bbs.ourga.com/viewthread.php?tid=449
[jass]
// 任意进制转换
function ConvRadix takes integer v, integer iRadix returns string
local string chSample = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\"
local integer cbSample = StringLength(chSample)
local string sRet = \"\"
local boolean bSign = false //是否是负数
local integer n // 商
local integer m // 余数
// Check Radix
if (iRadix < 2) and (iRadix > cbSample) then
return \"\"
endif
// Check -2147483648
if 0x80000000 == v then
return \"\"
endif
// 处理符号位
if v < 0 then
set v = - v
set bSign = true
endif
// Main
loop
// 运算
set n = v / iRadix
set m = v - n * iRadix
set v = n
// 连接字符串
set sRet = SubString(chSample, m, m + 1) + sRet
exitwhen 0 == v
endloop
if bSign then
set sRet = \"-\" + sRet
endif
return sRet
endfunction
// 任意进制转换(定长版)
function ConvRadixN takes integer v, integer iRadix, integer iCount returns string
local string s = ConvRadix(v, iRadix)
local integer m
set m = StringLength(s)
if m <= 0 then
elseif m < iCount then
if \"-\" != SubString(s, 0, 1) then
set s = StrDup(\"0\", iCount - m) + s
else
set s = SubString(s, 0, 1) + StrDup(\"0\", iCount - m + 1) + SubString(s, 1, m)
endif
endif
return s
endfunction
// 将任意进制字符串转为整数
function DecodeRadix takes string s, integer iRadix returns integer
local string chSample = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\"
local integer cbSample = StringLength(chSample)
local string sSrc = StrTrim(StringCase(s, true), \" \\t\", true, false)
local integer cbSrc = StringLength(sSrc)
local integer iRet = 0
local string byCur
local boolean bSign = false
local integer m = 0
local integer I
local integer J
// Check Radix
if (iRadix < 2) and (iRadix > cbSample) then
return 0
endif
// Main
set I = 0
loop
exitwhen I >= cbSrc
//
set byCur = SubString(sSrc, I, I + 1)
set J = 0
loop
exitwhen J >= iRadix
//
exitwhen SubString(chSample, J, J + 1) == byCur
//
set J = J + 1
endloop
if J < iRadix then
set m = iRet * iRadix + J
exitwhen m < 0 // Overflow
set iRet = m
else
if (false == bSign) and (\"-\" == byCur) then
set bSign = true
else
exitwhen true
endif
endif
//
set I = I + 1
endloop
if bSign then
set iRet = -iRet
endif
return iRet
endfunction
[/jass] |
|