其中接收到的那串十六进制数字节数是可变的
哪位高手指点下
vb串口通信中怎么把接收到的一串十六进制数放到一个动态数组中
答案:2 悬赏:10 手机版
解决时间 2021-02-06 22:44
- 提问者网友:箛茗
- 2021-02-06 07:23
最佳答案
- 五星知识达人网友:玩世
- 2021-02-06 07:45
在load里面定义一个 dim aa() as byte
再在接收里面重新定义一个
dim i as interge
redim aa(1-i)
再存入即可
再在接收里面重新定义一个
dim i as interge
redim aa(1-i)
再存入即可
全部回答
- 1楼网友:想偏头吻你
- 2021-02-06 08:35
'字符表示的十六进制数转化为相应的整数,错误则返回 -1
function converthexchr(str as string) as integer
dim test as integer
test = asc(str)
if test >= asc("0") and test <= asc("9") then
test = test - asc("0")
elseif test >= asc("a") and test <= asc("f") then
test = test - asc("a") + 10
elseif test >= asc("a") and test <= asc("f") then
test = test - asc("a") + 10
else
test = -1 '出错信息
end if
converthexchr = test
end function
'字符串表示的十六进制数据转化为相应的字节串,返回转化后的字节数
function strhextobytearray(strtext as string, bytbyte() as byte) as integer
dim hexdata as integer '十六进制(二进制)数据字节对应值
dim hstr as string * 1 '高位字符
dim lstr as string * 1 '低位字符
dim highhexdata as integer '高位数值
dim lowhexdata as integer '低位数值
dim hexdatalen as integer '字节数
dim stringlen as integer '字符串长度
dim account as integer
dim n as integer
'计数
'txtsend = "" '设初值
hexdatalen = 0
strhextobytearray = 0
stringlen = len(strtext)
account = stringlen \ 2
redim bytbyte(account)
for n = 1 to stringlen
do '清除空格
hstr = mid(strtext, n, 1)
n = n + 1
if (n - 1) > stringlen then
hexdatalen = hexdatalen - 1
exit for
end if
loop while hstr = " "
do
lstr = mid(strtext, n, 1)
n = n + 1
if (n - 1) > stringlen then
hexdatalen = hexdatalen - 1
exit for
end if
loop while lstr = " "
n = n - 1
if n > stringlen then
hexdatalen = hexdatalen - 1
exit for
end if
highhexdata = converthexchr(hstr)
lowhexdata = converthexchr(lstr)
if highhexdata = -1 or lowhexdata = -1 then '遇到非法字符中断转化
hexdatalen = hexdatalen - 1
exit for
else
hexdata = highhexdata * 16 + lowhexdata
bytbyte(hexdatalen) = hexdata
hexdatalen = hexdatalen + 1
end if
next n
if hexdatalen > 0 then '修正最后一次循环改变的数值
hexdatalen = hexdatalen - 1
redim preserve bytbyte(hexdatalen)
else
redim preserve bytbyte(0)
end if
if stringlen = 0 then '如果是空串,则不会进入循环体
strhextobytearray = 0
else
strhextobytearray = hexdatalen + 1
end if
end function
下面跟你介绍strhextobytearray(strtext as string, bytbyte() as byte)的功能。
假如text内输入"ff fe aa 14 af"(引号内的包括空格)那么
strhextobytearray=5
则bytebyte()中 bytebyte(0)=ff bytebyte(1)=fe bytebyte(2)=aa
bytebyte(3)=14 bytebyte(4)=af
注意:假如输入你想输入1,则必须写01
我想有了这个函数会给你很大的帮助。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯