关于字符串转二进制的问题

StandALoneComplex 2007-12-24 09:04:03
string ak="abcdefg"
blob b_ak
b_ak=blob(ak)
messagebox("单个值",long(blobmid(b_ak,1,1)))

类似于这样的一段代码,我把串变量ak转成二进制数据去取每个字节的值,可我逐字节的取出后发现值全是零,我不明白哪里做错了,字符串数据里绝对是有内容的,以messagebox("串的值",string(b_ak))输出也能转换回"abcdefg",
messagebox("单个值",char(blobmid(b_ak,1,1))) 以单字符转换输出也全是空,不知道数据转到哪去了
...全文
408 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
meiZiNick 2008-04-30
  • 打赏
  • 举报
回复
支持搂主,收藏
leio 2007-12-25
  • 打赏
  • 举报
回复

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

leio 2007-12-25
  • 打赏
  • 举报
回复
以前写过一个对象,你可以参考。
以下代码另存为uo_bit_operator.sru后导入PBL使用,需要pfc_n_cst_numerical,可以从PB的example里面抽取该对象。


$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



代码未完,后续
StandALoneComplex 2007-12-25
  • 打赏
  • 举报
回复
我大体明白为什么这样不行了,但需求还是要实现,怎么才能逐字节的读出一个字符串变量的数据呢

1,077

社区成员

发帖
与我相关
我的任务
社区描述
PowerBuilder 相关问题讨论
社区管理员
  • 基础类社区
  • WorldMobile
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧