比如有一串字符串s: aa 02 00 55
用十六进制显示是 61 61 20 30 32 20 30 30 35 35
我想实现的是如何把字符串s转换为另一字符串ss,然后ss的十六进制显示是aa 02 00 55
求s转换为ss的算法
用十六进制显示是 61 61 20 30 32 20 30 30 35 35
我想实现的是如何把字符串s转换为另一字符串ss,然后ss的十六进制显示是aa 02 00 55
求s转换为ss的算法
或A ~ F 之间的 减'A'的16进制(忘了是多少..) + 0xA 就行了.....你对每个字符判断转换一次就行了
CString s = "aa020055";
int nSize = s.GetLength();
char *pS = s.GetBuffer(nSize);
CString ss;
for(int i=0; i<nSize-1; i+=2)
{
char ch[2];
ch[0] = *(pS+i);
ch[1] = *(pS+i+1);
BYTE byVal = strtol(ch,NULL,16);
ss += byVal;
}
s.ReleaseBuffer();
把每个字符变成相应的ascall值
可以用AfxExtractSubString 以空格为分隔提取子字符串
已有字符串CString s = "aa020055"
要求转换成 0xaa020055
GetDlgItemText(IDC_SEND_DATA, strMsg);
int nSize = strMsg.GetLength();
char *pS = strMsg.GetBuffer(nSize);
CString ss;
for(int i=0; i<nSize-1; i++)
{
char ch[2];
ch[0] = *(pS+i);
ch[1] = *(pS+i+1);
BYTE byVal = strtol(ch,NULL,16);
ss += byVal;
}
strMsg.ReleaseBuffer();
int iSend = send(m_ConnectSock, (char *)ss.GetBuffer(0), ss.GetLength(), 0);
ss.ReleaseBuffer();
我用这个代码,接受方十六进制显示是A8280858
int iSend = send(m_ConnectSock, (char *)ss.GetBuffer(0), ss.GetLength(), 0);
下断点,看看ss的内存是否正确.正确的话就是接受方的问题
看清楚,
for(int i=0; i<nSize-1; i+=2)
int nSize = strMsg.GetLength();
char *pS = strMsg.GetBuffer(nSize);
CString ss;
for(int i=0; i<nSize-1; i=i+2)
{
char ch[2];
ch[0] = *(pS+i);
ch[1] = *(pS+i+1);
BYTE byVal = strtol(ch,NULL,16);
ss += byVal;
}
strMsg.ReleaseBuffer();
int iSend = send(m_ConnectSock, (char *)ss.GetBuffer(0), ss.GetLength(), 0);
ss.ReleaseBuffer();
aa发送到对方显示还是A8,对方是标准的程序,应该也不会错。提示我看到byVal的值也是对的
用你的程序,ss应该就是十六进制,输入aa,对方显示的却是A8
很奇怪
用你的算法发送字符串,输入abcd,显示是B8 D8
目前代码如下:
CString strMsg;
GetDlgItemText(IDC_SEND_DATA, strMsg);
int nSize = strMsg.GetLength();
char *pS = strMsg.GetBuffer(nSize);
CString ss;
for(int i=0; i<nSize-1; i=i+2)
{
char ch[2];
ch[0] = *(pS+i);
ch[1] = *(pS+i+1);
BYTE byVal = strtol(ch,NULL,16);
ss += byVal;
}
strMsg.ReleaseBuffer();
int iSend = send(m_ConnectSock, (char *)ss.GetBuffer(0), ss.GetLength(), 0);
ss.ReleaseBuffer();
把BYTE byVal = strtol(ch,NULL,16);
修改为int byVal = strtol(ch,NULL,16);
结果就对了,谢谢happyparrot
1.如果原始字符串除掉空格之后有奇数个字符的话....(第一次程序里没考虑空格,后来补上了)
2.如果原始字符中出现非十六进制字符的话....比如ST什么的,最后的字符串十六进制显示无论如何也得不到的
胡乱扫了几眼上面的帖子,居然忽略了最后楼主说问题解决的回复...悲剧,白白劳碌了...
下面是我自己写的程序,在VS2008下测试通过.发出来大家交流一下而已,毕竟浪费了一点脑细胞的说..
(写好就发出来了,完全没有优化,至于效率什么的大家就不要打击我了)
ps:本人目前还不会如何引用代码,求热心人指点一二.#include <stdio.h>
#include <string.h>
#include <ctype.h>int Func(const char *source, char *result);int main()
{
char testStr[] = "aa 02 00 55";
char resultStr[10];
Func(testStr, resultStr);
return 0;
}int Func(const char *source, char *result)
{
int length = strlen(source);
if((length + 1) % 3 != 0) //确保除掉空格之外还有偶数个字符
return -1;
int j = (length + 1) / 3;
for(int i=0; i<j; i++)
{
char c1 = toupper(source[3 * i]);
char c2 = toupper(source[3 * i + 1]);
if(!(isxdigit(c1) && isxdigit(c2))) //确保没有非十六进制的字符
return -1;
result[i] = (isdigit(c1) ? (c1 - '0') : (c1 - 'A' + 10)) * 16
+ (isdigit(c2) ? (c2 - '0') : (c2 - 'A' + 10));
}
result[j] = '\0';
return 0;
}说明:
1.上面帖子很多,没有细看,