二进制转换为十进制:
f_bintodec(string jeffrey)
return string
//-----------------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////
// This function convert the BIN code to DEC code.
// Write by JiangJeffrey on 20 Aug. 2002
////////////////////////////////////////////////////////////////
string ls_ss1,ls_ss2,ls_s1
int li_len,i,li_num,j
long li_ii2,li_ii1
ls_ss1=jeffrey
li_ii2=0
li_len=len(ls_ss1)
for i=li_len to 1 step -1
ls_s1=mid(ls_ss1,i,1)
if i=li_len then
if integer(ls_s1)=1 then
li_ii2=1
continue
else
li_ii2=0
continue
end if
end if
if integer(ls_s1)=1 then
li_num=li_len - i
li_ii1=1
for j = 1 to li_num
li_ii1=li_ii1*2
next
else
li_ii1=0
end if
li_ii2=li_ii2+li_ii1
next
ls_ss2=string(li_ii2)
return ls_ss2
十六进制转换为二进制
十六进制转换为二进制:
f_hextobin(string jeffrey)
return string
//---------------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////
// This function convert the HEX code to BIN code.
// Write by JiangJeffrey on 20 Aug. 2002
////////////////////////////////////////////////////////////////
int li_len,i
string ls_ss1,ls_ss2,ls_s1,ls_s2
ls_ss1=jeffrey
ls_s2=''
li_len=len(ls_ss1)
for i=1 to li_len
ls_s1=mid(ls_ss1,i,1)
if ls_s1='0' then
ls_s2='0000'
elseif ls_s1='1' then
ls_s2='0001'
elseif ls_s1='2' then
ls_s2='0010'
elseif ls_s1='3' then
ls_s2='0011'
elseif ls_s1='4' then
ls_s2='0100'
elseif ls_s1='5' then
ls_s2='0101'
elseif ls_s1='6' then
ls_s2='0110'
elseif ls_s1='7' then
ls_s2='0111'
elseif ls_s1='8' then
ls_s2='1000'
elseif ls_s1='9' then
ls_s2='1001'
elseif ls_s1='A' then
ls_s2='1010'
elseif ls_s1='B' then
ls_s2='1011'
elseif ls_s1='C' then
ls_s2='1100'
elseif ls_s1='D' then
ls_s2='1101'
elseif ls_s1='E' then
ls_s2='1110'
elseif ls_s1='F' then
ls_s2='1111'
end if
ls_ss2=ls_ss2+ls_s2
next
return ls_ss2
十进制转换为十六进制
f_dectohex(string as_dec)
return string
///////////////////////////////////////////////////////////
// This function convert the DEC code to HEX code.
// Refer to: f_hex2dec
//////////////////////////////////////////////////////////
string hh
integer i,iv,i1,i2
for i = 1 to len(as_dec)
iv = asc(mid(as_dec,i,1))
if iv > 255 or iv < 1 then
return ''
end if
i1 = iv /16
i2 = mod(iv,16)
if i1 > 9 then
hh = hh + char(55 + i1)
else
hh = hh + string(i1)
end if
if i2 > 9 then
hh = hh + char(55 + i2)
else
hh = hh + string(i2)
end if
next
return hh
十进制转换为十六进制
f_dectohex(string as_hex)
return string
///////////////////////////////////////////////////////////
// This function convert the HEX code to DEC code
// Refer to: f_dec2hex
//////////////////////////////////////////////////////////
string dd,hh,h1,h2
integer i,kk
for i = 1 to len(as_hex) / 2
hh = upper(mid(as_hex,i*2 - 1,2))
h1 = left(hh,1)
h2 = right(hh,1)
if h1 >='0' and h1 <= '9' then
kk = integer(h1) * 16
else
if h1 >= 'A' and h1 <= 'F' then
kk = (asc(h1) - 55) * 16 // 10 + asc(h1) - asc("A")
else
return ''
end if
end if
if h2 >= '0' and h2 <= '9' then
kk += integer(h2)
else
if h2 >= 'A' and h2 <= 'F' then
kk += asc(h2) - 55
else
return ''
end if
end if
if kk < 1 then
return ''
end if
dd = dd + char(kk)
next
return dd
十进制转换为二进制:
f_dectobin(integer jeffrey)
return string
string ls_bin
integer i,li_1,li_2
do
li_1 = jeffrey / 2
li_2 = mod(jeffrey,2)
if li_2 = 0 and li_1 = 0 then
exit
end if
ls_bin = string(li_2) + ls_bin
jeffrey = li_1
loop until (li_2 = 0 and li_1 = 0)
return ls_bin
进制转化的通用函数(绝对精彩)
//====================================================================
// [FUNCTION] : gf_convertnumsys()
// [RETURNS] : string
// [ARGEMENTS] :
// value integer ai_value 需要转化的10进制数
// value integer ai_how 转化方式(共3种: 2,8,16)
//--------------------------------------------------------------------
// [DESCRIPTION]: 把10进制转化成2,8,16进制(10进制数必须大于零)
//--------------------------------------------------------------------
// [AUTHOR INFO] : DoItNow Date: 2003.05.21
// [MODIFIER INFO]:
//
//--------------------------------------------------------------------
// Copyright (C) saccsoft.com(TM) 2003, All rights reserved
//====================================================================
String two,sixteen,eight //定义三个字符串变量,用来存放最后输出的进制值
String ls_return
Long lentwo,duan,i,j
String aa[50],a[50],b[4]
//====================================================================
// 先把10进制数据通过取余数转换成2进制
// 10进制到2进制的转换是通过取余数实现
//====================================================================
DO WHILE ai_value > 0
two = two + String(Mod(ai_value,2))
ai_value = ai_value / 2
LOOP
//====================================================================
// 选择输出进制
//====================================================================
CHOOSE CASE ai_how
CASE 2
//由于上面得到的2进制的数据方向是相反的:11011101101,所以要通过reverse()函数重新//把它颠倒过来
two = Reverse(two) //得到正确的2进制10110111011
ls_return= two
CASE 8
//2进制到8进制的转换是这样的,从右往左每3位为一个段,即8进制的一位,
lentwo = Len(two) //取2进制的长度
IF Mod(lentwo,3) > 0 THEN //分段
duan = lentwo / 3 + 1
ELSE
duan = lentwo /3
END IF
FOR i = duan TO 1 STEP -1 //由于没有颠倒过来的2进制是从右往左的,所以//分出来的段从左往右就是由大到小
aa[duan] = Mid(two,i * 3 - 2 ,3) //取出每一段的2进制值
a[duan] = '0'
FOR j = 1 TO 3
b[j] = Mid(aa[duan],j,1) //每一段从右往左取值
IF b[j] <> '' THEN
a[duan] = String(Integer(a[duan]) + 2^(j -1 ) * Integer(b[j]))
//按照2^0 * x + 2^1 *x +2^2 * x
//x为分别为从右到左的2进制的值,累加得到每一段的8进制值
END IF
NEXT
eight = eight + a[duan] //8进制段累加得到最后的8进制值
NEXT
ls_return= eight
CASE 16
//16进制的方法如同8进制,只是每4位为一段而已
lentwo = Len(two)
IF Mod(lentwo,4) > 0 THEN
duan = lentwo / 4 + 1
ELSE
duan = lentwo /4
END IF
FOR i = duan TO 1 STEP -1
aa[duan] = Mid(two,i * 4 - 3 ,4)
a[duan] = '0'
FOR j = 1 TO 4
b[j] = Mid(aa[duan],j,1)
IF b[j] <> '' THEN
a[duan] = String(Integer(a[duan]) + 2^(j -1 ) * Integer(b[j]))
END IF
CHOOSE CASE Integer(a[duan])
CASE 10
a[duan] = 'A'
CASE 11
a[duan] = 'B'
CASE 12
a[duan] = 'C'
CASE 13
a[duan] = 'D'
CASE 14
a[duan] = 'E'
CASE 15
a[duan] = 'F'
END CHOOSE
NEXT
sixteen = sixteen + a[duan]
NEXT
ls_return= sixteen
CASE ELSE
END CHOOSE
RETURN ls_return
10进制转化为2至任意进制的通用算法
目前支持1~9 A~Z 共36进制
36进制之后我不知用什么符号来表示
如果你想增加进制,只须更改数组 String lsa_JzMax[35]
$PBExportHeader$mf_dec_to_36jz.srf
$PBExportComments$10进制转化为36进制中的任一进制
global type mf_dec_to_36jz from function_o b j e c t
end type
forward prototypes
global function string mf_dec_to_36jz (unsignedlong al_10jz, integer ai_how)
end prototypes
global function string mf_dec_to_36jz (unsignedlong al_10jz, integer ai_how);// ------------------------------------------------------------------------
// 10进制转化为36进制中的任一进制
// 参数:Ulong al_10Jz 要转换的10进制数
// integer ai_how 转换为哪一进制:从2进制到36进制
// 返回:String 空字符串:出错,否则为转换后的结果
// ------------------------------------------------------------------------
// 说明:这是一个通用的