用什么函数才可以将二进制的数据转为0、1的字符串呢?

cafefish 2005-10-16 04:24:19
例如在串口接收到的Byte数据为01010101,现在需要将其转成String字符串"0,1,0,1,0,1,0,1",该如何实现呢?
...全文
228 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jxgzay 2005-10-16
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
x = Text1.Text
l = Len(x)
y = 0
For i = 1 To l
y = Val(Mid(x, i, 1)) * 2 ^ (l - i) + y
Next

x = Hex(y)
x = Replace(x, "0", "0000")
x = Replace(x, "1", "0001")
x = Replace(x, "2", "0010")
x = Replace(x, "3", "0011")
x = Replace(x, "4", "0100")
x = Replace(x, "5", "0101")
x = Replace(x, "6", "0110")
x = Replace(x, "7", "0111")
x = Replace(x, "8", "1000")
x = Replace(x, "9", "1001")
x = Replace(x, "A", "1010")
x = Replace(x, "B", "1011")
x = Replace(x, "C", "1100")
x = Replace(x, "D", "1101")
x = Replace(x, "E", "1110")
x = Replace(x, "F", "1111")

MsgBox "十进制:" & y & vbCrLf & "十六进制:" & Hex(y) & vbCrLf & "二进制" & x
End Sub
province_ 2005-10-16
  • 打赏
  • 举报
回复
楼上的可以,只是最后的逗号要去掉。
flyhigh 2005-10-16
  • 打赏
  • 举报
回复
dim str as String
dim input as integer
for i=1 to 8
str=str+ (input mod 2) +","
input=input/2
next i

msgbox str
northwolves 2005-10-16
  • 打赏
  • 举报
回复
Dim x() As String, i As Integer
ReDim x(UBound(b))
For i = 0 To UBound(b)
x(i) = CStr(b(i))
Next
MsgBox Join(x, ",")
balloonman2002 2005-10-16
  • 打赏
  • 举报
回复
首先BYTE数据01010101对应的是十进制数:85,进而可以利用下文方法:
http://www.zdnet.com.cn/developer/common/printfriend/printfriendly.htm?AT=39249246-3800066897t-20000560c
把十进制转换为二进制输出即可
cafefish 2005-10-16
  • 打赏
  • 举报
回复
我是思路是:二进制==》十六进制==>字符串,这样正确吗?
不知是否有现成的代码呢?
ExeMan 2005-10-16
  • 打赏
  • 举报
回复
cstr(True)
Summer006 2005-10-16
  • 打赏
  • 举报
回复
好像没有转换二进制字串的函数,只有十六禁止的hex。
自己写一个吧

7,763

社区成员

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

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