1,077
社区成员
发帖
与我相关
我的任务
分享
public function string int2hex (unsignedlong ai);string hex
ai = Truncate (ai,0)
do while ai > 0
if mod(ai,10) >= 10 then
hex=char(asc("A")+mod(ai,10) - 10)+hex
else
hex=string(mod(ai,10))+hex
end if
ai = Truncate(ai/10,0)
loop
return hex
end function
public function unsignedlong hex2int (string hex);long l_len,l,l_lenmax,ll
decimal hex_to_dec,dec_x
l_len=len(trim(hex))
l=1
for l=1 to l_len
if asc(mid(hex,l,1))>=71 then //asc('G')=71
return 0
end if
next
l=1
l_lenmax=l_len -1
do while l_lenmax>=0
if asc(upper(mid(hex,l,1)))>=65 and asc(upper(mid(hex,l,1)))<=70 then //asc('A')=65;asc('F')=70
ll=asc(upper(mid(hex,l,1))) - 55 //asc('A')-55=10
else
ll=long(mid(hex,l,1))
end if
dec_x=16^l_lenmax
hex_to_dec=hex_to_dec+ll*dec_x
l_lenmax=l_lenmax -1
l=l+1
loop
return truncate(hex_to_dec,0)
end function
on uo_bit_operator.create
call super::create
TriggerEvent( this, "constructor" )
end on
on uo_bit_operator.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on
event constructor;//Create By Leio 张
//2005.11
//int与byte类型互换
//PB中string,char类型均不能很好地处理h00,只好使用BLOB类型
//这个UO为此而作
//
end event
$PBExportHeader$uo_bit_operator.sru
forward
global type uo_bit_operator from nonvisualobject
end type
end forward
global type uo_bit_operator from nonvisualobject autoinstantiate
end type
type variables
pfc_n_cst_numerical pfc_numerical
end variables
forward prototypes
public function unsignedlong bytestoint (character abyte[])
public function unsignedlong bytetoint (character abyte)
public subroutine blob_fill (ref blob a_blob, integer a_fill)
public function boolean blob_modify (ref blob a_blob, integer ai_start, integer ai_length, string as_replace)
public function unsignedlong bytes4toint (character abyte[])
public function string inttobytes4 (unsignedlong ai, ref character abyte[4])
public function character inttobyte (unsignedlong ai, ref character abyte)
public function string inttobytes (unsignedlong ai, ref character abyte[2])
public subroutine bytescopy (character abyte0[], ref character abyte1[], long i, long j, long k)
public function string longtobytes4 (longlong al, ref character abyte[4])
public function longlong bytes4tolong (character abyte[])
public subroutine blob2chararray (blob blobbuf, ref character chararray[])
public subroutine chararray_replace (ref character a_char1[], character a_char2[], long al_start, long al_end)
public subroutine blob_edit (ref blob blobbuf, unsignedlong i, unsignedlong j, character chararray[])
public function string int2hex (unsignedlong ai)
public function unsignedlong hex2int (string hex)
end prototypes
public function unsignedlong bytestoint (character abyte[]);//return ((0xff & abyte0[0]) << 8) + abyte0[1]
//2字节转成int
return (pfc_numerical.of_bitwiseand(255,asc(abyte[1])) * 256) + asc(abyte[2])
end function
public function unsignedlong bytetoint (character abyte);//单字节转成int
return asc(abyte)
end function
public subroutine blob_fill (ref blob a_blob, integer a_fill);//a_blob BLOB变量,BLOB长度需要先设置好
//a_fill 初始化BLOB填充的内容(ASCII码数字)
long i
a_blob = Blob(Space(len(a_blob)))
blob{1} bl_temp
bl_temp = blob(char(a_fill))
if a_fill = 0 then
for i = 1 to Len(a_blob)
BlobEdit(a_blob, i, char(0))
next
else
for i = 1 to Len(a_blob)
BlobEdit(a_blob, i, bl_temp)
next
end if
end subroutine
public function boolean blob_modify (ref blob a_blob, integer ai_start, integer ai_length, string as_replace);//a_blob 需要处理的BLOB
//ai_start 开始位置
//ai_length 需要替换的长度
//as_replace 需要替换的字符串,不可为空,其长度应不大于ai_length
character lch_temp[]
lch_temp = as_replace
if isnull(as_replace) or as_replace = '' then return true //不需要修改
if (len(a_blob) - ai_start) < ai_length then return false //剩余长度不够
if len(as_replace) > ai_length then return false //超长
if ai_start < 1 then return false //起始位不对
//blobedit(a_blob,ai_start,blob(as_replace))
blob_edit(a_blob,ai_start,ai_start+ai_length - 1,lch_temp)
return true
end function
public function unsignedlong bytes4toint (character abyte[]);//将4位byte转成32位数字
//return (0xff & abyte0[0]) << 24 | (0xff & abyte0[1]) << 16 | (0xff & abyte0[2]) << 8 | 0xff & abyte0[3];
ulong l_abyte1,l_abyte2,l_abyte3,l_abyte4,l_return
l_abyte1 = pfc_numerical.of_bitwiseand(255,asc(abyte[1])) * 16777216
l_abyte2 = pfc_numerical.of_bitwiseand(255,asc(abyte[2])) * 65536
l_abyte3 = pfc_numerical.of_bitwiseand(255,asc(abyte[3])) * 256
l_abyte4 = pfc_numerical.of_bitwiseand(255,asc(abyte[4]))
l_return = pfc_numerical.of_bitwiseor(l_abyte1,l_abyte2)
l_return = pfc_numerical.of_bitwiseor(l_return,l_abyte3)
l_return = pfc_numerical.of_bitwiseor(l_return,l_abyte4)
return l_return
end function
public function string inttobytes4 (unsignedlong ai, ref character abyte[4]);//int转成4字节
if ai < 0 or ai > 4294967295 then //FFFFFFFF=4294967295
return ''
end if
character abyte0[4]
abyte0[4] = char(pfc_numerical.of_bitwiseand(255,ai))
abyte0[3] = char(pfc_numerical.of_bitwiseand(65280,ai) / (2^8))
abyte0[2] = char(pfc_numerical.of_bitwiseand(16711680,ai) / (2^16))
abyte0[1] = char(pfc_numerical.of_bitwiseand(4278190080,ai) / (2^24))
abyte = abyte0
return abyte0
end function
public function character inttobyte (unsignedlong ai, ref character abyte);//int转成单字节
character byte_temp[2]
if ai >= 0 and ai <= 255 then
abyte = char(ai)
else
inttobytes(ai,byte_temp)
abyte = byte_temp[2]
end if
//abyte = char(ai)
return abyte
end function
public function string inttobytes (unsignedlong ai, ref character abyte[2]);//int转成双字节
char abyte0[2],byte_temp[4]
if ai >= 0 and ai <= 65535 then
abyte0[2] = char(pfc_numerical.of_bitwiseand(255,ai))
abyte0[1] = char(pfc_numerical.of_bitwiseand(65280,ai) / (2^8))
else
inttobytes4(ai,byte_temp)
abyte0[2] = byte_temp[4]
abyte0[1] = byte_temp[3]
end if
abyte = abyte0
return abyte0
end function
public subroutine bytescopy (character abyte0[], ref character abyte1[], long i, long j, long k);//按字节拷贝
//将abyte0拷贝到abyte1中指定的位置。
//i~j abyte0中要拷贝的字节。
//k abyte1中拷贝起始位置
if upperbound(abyte0) < 1 then return
if i > j or k < 1 then return
if k <= 0 then k = 1
long i1,l
i1 = 0
for l = i to j
abyte1[k+i1] = abyte0[l]
i1++
next
end subroutine
public function string longtobytes4 (longlong al, ref character abyte[4]);//长整型转成4字节
char abyte0[4]
abyte0[4] = char(pfc_numerical.of_bitwiseand(255,al))
abyte0[3] = char(pfc_numerical.of_bitwiseand(65280,al) / (2^8))
abyte0[2] = char(pfc_numerical.of_bitwiseand(16711680,al) / (2^16))
abyte0[1] = char(pfc_numerical.of_bitwiseand(4278190080,al) / (2^24))
abyte = abyte0
return abyte0
end function
public function longlong bytes4tolong (character abyte[]);//将4位byte转成32位数字
//return (0xff & abyte0[0]) << 24 | (0xff & abyte0[1]) << 16 | (0xff & abyte0[2]) << 8 | 0xff & abyte0[3];
ulong l_abyte1,l_abyte2,l_abyte3,l_abyte4,l_return
l_abyte1 = pfc_numerical.of_bitwiseand(255,asc(abyte[1])) * 16777216
l_abyte2 = pfc_numerical.of_bitwiseand(255,asc(abyte[2])) * 65536
l_abyte3 = pfc_numerical.of_bitwiseand(255,asc(abyte[3])) * 256
l_abyte4 = pfc_numerical.of_bitwiseand(255,asc(abyte[4]))
l_return = pfc_numerical.of_bitwiseor(l_abyte1,l_abyte2)
l_return = pfc_numerical.of_bitwiseor(l_return,l_abyte3)
l_return = pfc_numerical.of_bitwiseor(l_return,l_abyte4)
return l_return
end function
public subroutine blob2chararray (blob blobbuf, ref character chararray[]);long i
blob{1} blob_temp
character char_null[]
chararray = char_null
if len(blobbuf) <= 0 then return
for i = 1 to len(blobbuf)
blob_temp = blobmid(blobbuf,i,1)
chararray[i] = char(blob_temp)
next
end subroutine
public subroutine chararray_replace (ref character a_char1[], character a_char2[], long al_start, long al_end);//把a_char1中从al_start~al_end用a_char2替换掉。
long i,j
if upperbound(a_char2) < 1 then return
if upperbound(a_char2) < al_end - al_start then return
j = 1
for i = al_start to al_end
a_char1[i] = a_char2[j]
j++
next
end subroutine
public subroutine blob_edit (ref blob blobbuf, unsignedlong i, unsignedlong j, character chararray[]);//blob长度需要预先设好。
//i->blobbuf的起始字节
//j->blobbuf的结束字节
//i~j字节内容用chararray替换
if i = 0 then i = 1
if j = 0 then j = 1
if upperbound(chararray) < 1 then return
if upperbound(chararray) < j - i then return
if len(blobbuf) < j then return
if i>j then return
ulong i_loop,k
k=1
for i_loop = i to j
if asc(chararray[k]) = 0 or isnull(chararray[k]) then
blobedit(blobbuf,i_loop,char(0))
else
blobedit(blobbuf,i_loop,chararray[k])
end if
k++
next
end subroutine