先描述一下情况 服务端发送AA 本人不选中checkHewView 显示AA 然后再选中十六进制显示 服务器发送BB 第一下显示BB 之后就是42 42了 各位高手知道问题出在哪里吗 下面附源码
while (true) { if (checkHewView.Checked) { byte[] buffer = new byte[1024 * 1024]; int n = client.Receive(buffer); string word = Encoding.UTF8.GetString(buffer, 0, n); char[] values = word.ToCharArray(); string words = null; foreach (char letter in values) { int value = Convert.ToInt32(letter); words = String.Format("{0:X}", value); ShowMsg(words); } // ShowMsg(word); } else { //通信用Socket try { byte[] buffer = new byte[1024 * 1024]; int n = client.Receive(buffer); string words = Encoding.UTF8.GetString(buffer, 0, n); ShowMsg(words); }
解决方案
40
1、代码
string word = Encoding.UTF8.GetString(buffer, 0, n); char[] values = word.ToCharArray(); string words = null; foreach (char letter in values) { int value = Convert.ToInt32(letter); words = String.Format("{0:X}", value); ShowMsg(words); }
可直接改成
string word = BitConverter.ToString(buffer).Replace("-", " "); ShowMsg(words);
2、估计收不到数据会阻塞在client.Receive(buffer);处,选中checkHewView时已经在else里了,收到第一次还是非十六进制显示
3、把client.Receive(buffer);放在if之前应该不会出问题了