vc socket的接收端解析一串字符串
字符串例子如下:原始数据例子:
02 18 00 4C 4A 49 41 4A 01 27 0C 22 38 4E 0A 5E A1 13 1F 3D 4C 48 40 FF 01 00 00 03
包头: 02
数据长度: 18 00 = 00 18 = 10进制= 24s
命令类型:4C
数据内容:4A 49 41 4A 01 27 0C 22 38 4E 0A 5E A1 13 1F 3D 4C 48 40 4E
终端编号: 4A 49 41 4A = 74736574
SIM卡号: 01 27 0C 22 38 4E = 013912345678
终端 IP : 0A 5E A1 13 =10.94.161.19
信号质量: 1F = 31
司机工号: 3D 4C 48 40 4E = 61767264
LED版本号:FF 01 00 00 = 00 00 FF 01 = 10 进制化=511
包尾:03
大家有什么好的意见,想法,给俺指点指点,谢谢大家啦!
[解决办法]
慢慢按长度一段段截取
字符串转16进制
比较繁杂罢了
===============
16进制转换:
#include <iostream.h>#include <afxwin.h>int HexstringToInt(CString result);void main(){ CString str = "01 0E 7B 22 18"; CString temp1 = str.Mid(6,2); CString temp2 = str.Mid(9,2); CString temp3 = str.Right(2); CString result = temp3 + temp2 + temp1; cout<< str <<endl; cout<< result <<endl; result = "14aFDEC8"; cout<< HexstringToInt(result) <<endl; }int HexstringToInt(CString result){ int val = 0; int sum = 0; for (int i=0;i<result.GetLength();i++) { char ch = result.GetAt(i); if (ch>=65&&ch<=70) { val = ch-55; } else if (ch>=97&&ch<=102) { val = ch-87; } else if (ch>=48&&ch<=57) { val = ch-48; } else { cout<< "ERROR Prameter" <<endl; sum = 0 ; break; } val = val<<((result.GetLength()-1-i)*4); sum += val; } return sum;}
[解决办法]
不知道有多少前人掉在TCP Socket
send(人多)send(病少)send(财富)
recv(人多病)recv(少财富)
陷阱里面啊!
http://topic.csdn.net/u/20120210/09/51109ed0-07b9-41f2-b487-a51597f2ca01.html