如何将char结构数据转换为cstring? [英] How to covert char struct data to cstring?

查看:118
本文介绍了如何将char结构数据转换为cstring?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将struct variable path_name转换为CString,如下所示:



I want to convert struct variable path_name to CString like as follows:

typdef struct path_name{
   	char mac[12]; // mac-address
	char dot1;
	char date[8];  
	char dot2;
	char portnum[2]; 
	char dot3;
	char fileseq[2]; 
	char dot4;
	char ext[3];     
} l_n;





如果我试图做错会导致错误



CString myPathname = CString(path_name);



我尝试过:



几乎整天浪费了这次转换



It makes error if I tried to do

CString myPathname = CString(path_name);

What I have tried:

Almost my whole day wasted for this conversion

推荐答案

这里有三个问题:

There are three problems here:


  1. 无法将结构转换为字符串。
  2. 结构成员的类型为 char ,而 CString 使用 TCHAR s。这在使用Unicode构建时会出现问题。
  3. 结构成员似乎不会以NULL结尾,因此无法确定长度。

  1. A structure can not be converted to a string.
  2. The struct members are of type char while CString uses TCHARs. This will be a problem when using Unicode builds.
  3. The struct members seem not to be NULL terminated so that the length can't be determined.





第三个问题的可能解决方案是使用 CSimpleStringT :: SetString [ ^ ]方法长度可以通过。由于第一个问题,必须对每个不是单个字符的结构元素进行此操作。使用Unicode构建时,必须使用 CStringA



A possible solution for the third problem is using the CSimpleStringT::SetString[^] method where the length can be passed. Due to the first problem, this must be done for each structure element that is not a single character. When using Unicode builds, this must be done using CStringA:

CStringA strMac, strDate, strPortNum, strFileSeq, strExt;
strMac.SetString(pathName, sizeof(path_name.mac));
strDate.SetString(pathName, sizeof(path_name.date));
strPortNum.SetString(pathName, sizeof(path_name.portnum));
strFileSeq.SetString(pathName, sizeof(path_name.fileseq));
strExt.SetString(pathName, sizeof(path_name.ext));

CString myPathName;
myPathName.Format(_T("%hs%hc%hs%hc%hs%hc%hs%hc%hs"),
    strMac.GetString(), pathName.dot1,
    strDate.GetString(), pathName.dot2,
    strPortNum.GetString(), pathName.dot3,
    strFileSeq.GetString(), pathName.dot4,
    strExt.GetString());



注意'h'前缀的用法,表示参数的类型为 char 字符* 。这确保了上面的代码也可以用于Unicode构建。



如果你知道你的结构只包含字符并且编译器没有插入填充字节,您可以通过将结构复制到缓冲区,附加一个NULL字节并将其转换为 CString 来使用更简单的代码:


Note the usage of the 'h' prefix which indicate that the parameter is of type char or char*. This ensures that the above code can be also used with Unicode builds.

If you know that your structure contains only characters and there are no fill bytes inserted by the compiler, you can use a simpler code by copying the structure to a buffer, appending a NULL byte and converting that to a CString:

char *buffer = new char[sizeof(path_name) + 1];
memcpy(buffer, &pathName, sizeof(path_name));
buffer[sizeof(path_name)] = '\0';
// This will convert to Unicode with Unicode builds
CString myPathName(buffer);
delete [] buffer;


您无法转换类型 struct path_name )到变量( myPathname )。你所能做的就是将一个变量(一个类型的实例)转换为另一个变量(另一个类型的实例)。

所以你可能会做类似的事情

You cannot convert a type (struct path_name) to a variable (myPathname). All you can do is converting a variable (instance of a type) to another variable (instance of another type).
So you might do something like
struct path_name path;

// initialize path...

CString myPathname = CString( (const char *) path);





然而,我几乎不相信这种转换有用。



However, I hardly believe such a conversion useful.


这篇关于如何将char结构数据转换为cstring?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆