如何把CString类型转化为array数组?
问题:CString 如何转化为 array数组?
问题补充:
我是用 MFC的edit框接收一组数据为CString类型的 放在 m_data里面我要用以下操作把这个 CString转化为 一个数值的 array数组
我的测试数据为 B2 52 F1 16 28 36 想出来的 array数组也是这个值
下面的代码就是取array的值
UpdateData();
CString m_tmpstr = m_data;
m_tmpstr += " ";
unsigned char crc = 0;
unsigned char crc1 = 0;
unsigned char crc2 = 0;
array = NULL;
char *tmp_str = NULL;
unsigned int con = 0;
tmp_str = m_tmpstr.GetBuffer(200);
while (*tmp_str)
{
if (*tmp_str == ' ')
con++;
tmp_str++;
}
array = (unsigned char *)malloc(sizeof(unsigned char) * con);
char tmp[6] = "";
char *ptmp;
tmp_str = m_tmpstr.GetBuffer(150);
unsigned int i;
for (i = 0; i < con; i++)
{
ptmp = tmp;
memset(tmp,'',sizeof(char) * strlen(tmp));
while (*tmp_str != ' ')
*ptmp++ = *tmp_str++;
array[i] = fun(tmp);
tmp_str++;
}
在这个函数里面的fun函数 如下
unsigned char fun(char *tmp)
{
char *pt = tmp;
unsigned char sum = 0;
while (*pt)
{
sum = sum * 16 + (*pt - '0');
pt++;
}
return sum;
}
每次测试的时候 数组里面的 F1就变成61了 请大家帮我看看 这个要怎么修改
我觉得主要要修改这个fun函数 这个fun函数主要实现对array的赋值
最后我要得到的数据是 从array里面出来的 array[2]和array[3]组成一个16位的值 然后这个值还要是有符号的!
解答:
这是用控制台方式编的,你从中提取有用部分。
// ttttttt.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include //CString的头文件
#include
#include
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int array[1000];
CString cs("B2 52 F1 16 28 36 ");//事例数据
istringstream istr;//利用流来操作
istr.str(cs.GetBuffer()); //从字符串转为流
int i=0,j;
while(!istr.eof())//检查是否结束
{
istr>>hex>>j;//从流读入数据,以十六进方式
if(istr.bad()) break;//检查是否读到有误的数据,比如事例里读到36后的空格
array[i++]=j;//存好数据,并准备下一个
}
for(j=0;j