以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- [求助]字符串转字节数组和DataRow集合比较请教蓝版谢谢 (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=172554) |
-- 作者:WELOVEFOX -- 发布时间:2021/10/19 11:50:00 -- [求助]字符串转字节数组和DataRow集合比较请教蓝版谢谢 最近在研究串口时,遇到几个问题: 1.字符串以某分割字符如何转字节数组 原成功操作代码 Dim b_sent() As Byte = {&H01,&H0F} \'定义字节数组 Ports("COM3").Write(b_sent, 0, b_sent.length) \'写入串口命令 如果需要转变成 Dim s As String = "&H01,&H0F" 目前是用 s = s.Replace("&H","") Dim strAry() As String = s.Split(",") Dim b_sent(strAry.Length-1) As Byte For i As Integer = 0 To strAry.Length - 1 b_sent(i) = Byte.Parse(strAry(i), System.Globalization.NumberStyles.HexNumber) Next Ports("COM3").Write(b_sent, 0, b_sent.length) 能否将s直接转换成字节数组b_sent() ,考虑过GetBytes(),不会套。 2. DataRow集合如何判断不同,翻看过其它帖子,没解决。 Dim drsa As List(of DataRow) = DataTables("数据库1").SQLSelect("列1 = true") Dim drsb As List(of DataRow) = DataTables("数据库2").SQLSelect("列2 = true") If drsa.count <> drsb.count Then Output.Show("不同") Else For i As Integer = 0 To drsa.count-1 \'If drsa(i) <> drsb(i) Then \'提示出错,不能直接比较 \' Output.Show("不同") \' End If Next End If |
-- 作者:有点蓝 -- 发布时间:2021/10/19 13:44:00 -- 1、直接转换即可 Dim s As String = "&H01,&H0F" Dim ar() As String = s.Split(",") For Each a As String In ar Dim d As Byte = a Output.Show(d & "," & Format(d,"x")) Next 2、datarow无法比较,请比较单元格的内容,比如 If drsa(i)("编号") <> drsb(i)("编号") Then |
-- 作者:WELOVEFOX -- 发布时间:2021/10/19 16:40:00 -- 谢谢蓝版。 1.字符串转字节数组,是转字节数组,不是转字符数组。我的意思是将一段字符串,以某分割符,直接转成字节数组。所以再请教下。 Dim s As String = "&H01,&H0F" Dim ar() As String = s.Split(",") For Each a As String In ar Dim d As Byte = a Output.Show(d & "," & Format(d,"x")) \'以16进制显示 Next 如何执行 Ports("COM3").Write(字节数组, 0, 字节数组.length) ?并且字符串可能是"&H01,&H0F" 或 "01 0F" 或 "01-0F",Split后,用Dim d As Byte = a不行 For Each a As String In ar Dim d As Byte = a Ports("COM3").Write(字节数组(d), 0, 字节数组(d).length) \'不会转-_-!! Next 2.成功解决DataRow集合比较异同,十分感谢! |
-- 作者:有点蓝 -- 发布时间:2021/10/19 17:44:00 -- 神啊,一个字符可以转换,一个数组也就一个遍历的事 Dim s As String = "&H01,&H0F" Dim ar() As String = s.Split(",") dim aa(ar.length-1) as byte For i as integer = 0 to ar.length-1 aa(i) = ar(i) Next |
-- 作者:WELOVEFOX -- 发布时间:2021/10/19 18:31:00 -- 谢谢蓝版,,最近研究这个有点头晕脑涨,也是用了FOR,其实是想看能不能简单点,用其它转换命令,像下面一样,看能不能直接一行搞定的,多了分割符号,转不过弯了。如 Dim txt As String = "你好"
Dim zj() As Byte = System.Text.Encoding.Default.GetBytes(txt) |