搜索
查看: 5931|回复: 8

wsock接收16进制数据后如何正确的添加到文本框中显示

[复制链接]

8

主题

124

帖子

905

积分

三级会员

Rank: 4

积分
905
发表于 2013-1-7 09:10:02 | 显示全部楼层 |阅读模式
最近弄一个调试辅助工具,服务器发送的应答是16进制格式,我需要在文本框中显示出16进制数据。我的写法是:
  1. //wsock读取处理函数
  2. netrecvcommand = function(nSocket) begin
  3.         var reads,recvbuf = wsock.recv(        nSocket,2048,2048,0);
  4.         if ( reads=-1 ){
  5.                  //io.print( "客户端接收错误");
  6.                  //io.print( wsock.err.lasterr() );
  7.                  return false;
  8.         }
  9.         if(mainForm.radiobutton_Aiscii.checked == true)
  10.           mainForm.edit2.appendText(recvbuf);
  11.         else {
  12.          // mainForm.edit2.appendText( string.fromUnicode(recvbuf));
  13.           //mainForm.edit2.appendText(tostring(recvbuf,16));
  14.           io.print("收到的数据是:" ,recvbuf);
  15.           mainForm.edit2.appendText(string.format("0x%X",tostring(recvbuf,16)));
  16.          }
  17.         return true;
  18. end;
复制代码
但是上面接收后始终只能显示一个字节,不能显示其全部,但是这个字节也不是服务器发送过来的那个字节,
服务器发送应答(HEX格式):FE 00 00 00 12 03 0E FC
我的客户端接收后就不能显示出来,如果服务器发的是aiscii命令就可以正常显示,希望哪位指点一下,谢谢。
回复

使用道具 举报

2

主题

871

帖子

4689

积分

荣誉会员

Rank: 8Rank: 8

积分
4689
发表于 2013-1-7 09:29:46 | 显示全部楼层

%X 格式化的是一个数值,你传入的是字符串,可想而知结果是错误的了。 tostring(recvbuf,16) 这个函数指定16进制时,他的作用类似%X把

%X 格式化的是一个数值,你传入的是字符串,可想而知结果是错误的了。

tostring(recvbuf,16) 这个函数指定16进制时,他的作用类似%X把一个数值转换为16进制,可是你传进去的参数是一个字符串,可想而知也是错知的了。

如果你想转换为上面的格式,参考一下 inet.url.encode,做一个简单的转换,如下:
  1. hex = function(s){
  2.     return ( string.replace(s,".",
  3.         function(c){  
  4.                 return string.format(" %02X", c[1] )
  5.         }
  6.     ) )
  7. }

  8. io.open()
  9. io.print( hex("这是接收到的字符串") )
复制代码
回复

使用道具 举报

8

主题

124

帖子

905

积分

三级会员

Rank: 4

积分
905
 楼主| 发表于 2013-1-7 11:16:09 | 显示全部楼层

谢谢“不净”的指导 不过现在遇到一个问题,服务器发送:FE 03 04 01 FC 01 而我用你的这个函数处理过后能显示出来了, 显示结果: FE

谢谢“不净”的指导
不过现在遇到一个问题,服务器发送:FE 03 04 01 FC 01
而我用你的这个函数处理过后能显示出来了,
显示结果:
FE 03 04 01 FC 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
我就不明白后面怎么出来那么多个字节,这个是与我设置的接收缓冲区有关吗?请指点一下,谢谢。
回复

使用道具 举报

2

主题

871

帖子

4689

积分

荣誉会员

Rank: 8Rank: 8

积分
4689
发表于 2013-1-7 19:54:31 | 显示全部楼层

这是按缓冲区来发送的,多出字节应当是正常的,看你服务端的代码怎么写的了

这是按缓冲区来发送的,多出字节应当是正常的,看你服务端的代码怎么写的了
回复

使用道具 举报

8

主题

124

帖子

905

积分

三级会员

Rank: 4

积分
905
 楼主| 发表于 2013-1-7 20:01:06 | 显示全部楼层

[i=s] 本帖最后由 renmin 于 2013-1-7 20:04 编辑 [/i] [quote][size=2][color=#999999]不争 发表

本帖最后由 renmin 于 2013-1-7 20:04 编辑
不争 发表于 2013-1-7 19:54
这是按缓冲区来发送的,多出字节应当是正常的,看你服务端的代码怎么写的了


var reads,recvbuf = wsock.recv( nSocket,2048,2048,0);
我感觉是这个地方的问题,因为我将缓冲区设置为1024,后面解析出来的00字节就没有那么多了
但是我不明白是不是将整个缓冲区都给转换了?
我使用其他软件测试了,服务器是没有问题的,服务器是使用人家成熟的,问题就出在我的客户端上面。
回复

使用道具 举报

2

主题

871

帖子

4689

积分

荣誉会员

Rank: 8Rank: 8

积分
4689
发表于 2013-1-7 20:06:54 | 显示全部楼层

你思考一下 reads 是起什么作用的,于是你会找到答案的。

你思考一下 reads 是起什么作用的,于是你会找到答案的。
回复

使用道具 举报

8

主题

124

帖子

905

积分

三级会员

Rank: 4

积分
905
 楼主| 发表于 2013-1-7 20:18:25 | 显示全部楼层

[i=s] 本帖最后由 renmin 于 2013-1-7 20:26 编辑 [/i] [quote][size=2][color=#999999]不争 发表

本帖最后由 renmin 于 2013-1-7 20:26 编辑
不争 发表于 2013-1-7 20:06
你思考一下 reads 是起什么作用的,于是你会找到答案的。


我在回去仔细研究一下那个例子客户端看看,谢谢

那个好像就是一个容错的吧,接收发生错误就会返回
不是很理解
回复

使用道具 举报

2

主题

871

帖子

4689

积分

荣誉会员

Rank: 8Rank: 8

积分
4689
发表于 2013-1-8 13:21:04 | 显示全部楼层

reads 就是读取到的长度

reads  就是读取到的长度
回复

使用道具 举报

8

主题

124

帖子

905

积分

三级会员

Rank: 4

积分
905
 楼主| 发表于 2013-1-8 13:48:58 | 显示全部楼层

谢谢“不净”的一直指导。 问题得到解决 只需要取出数据长度就可以了 string.left(recvbuf,reads) 具体的代码如下:[code]/

谢谢“不净”的一直指导。
问题得到解决
只需要取出数据长度就可以了
string.left(recvbuf,reads)
具体的代码如下:
  1. //wsock读取处理函数
  2. netrecvcommand = function(nSocket) begin
  3.         //var reads,recvbuf = wsock.recv(        nSocket,2048,2048,0);
  4.         var reads,recvbuf = wsock.recv(        nSocket,1024,1024,0);
  5.         if ( reads=-1 ){
  6.                  //io.print( "客户端接收错误");
  7.                  //io.print( wsock.err.lasterr() );
  8.                  return false;
  9.         }
  10.         if(mainForm.radiobutton_Aiscii.checked == true)
  11.           mainForm.edit2.appendText(string.left(recvbuf,reads));//string.left(recvbuf,reads)这个是老大提醒的
  12.         else {
  13.           io.print("收到的数据是:" ,hex(string.left(recvbuf,reads)));
  14.           mainForm.edit2.appendText(hex(string.left(recvbuf,reads)));
  15.          }
  16.         return true;
  17. end;

  18. //16进制处理函数
  19. hex = function(s){
  20.     return ( string.replace(s,".",
  21.         function(s){
  22.                 return string.format("%02X ", s[1])
  23.         }
  24.     ) )
  25. }
复制代码
希望后面的朋友不要走弯路。
问题到此结束。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

手机版|未经许可严禁引用或转载本站文章|站长邮箱|aardio.com|aardio官方社区 ( 皖ICP备09012014号 )

GMT+8, 2020-8-15 12:48 , Processed in 0.062500 second(s), 22 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表