Creo二次开发——字符串操作函数总结

2025-10-08 08:09:38

1、字符串转换函数

ProStringToWstring--用来将标准ASCII字符串转化为宽字符

ProWstringToString--用来将宽字符转化为标晃板胳准ASCII字符

ProStringToLegacystring--将Unicode的多字节字符串转换为传统的编码方式

ProWstringToLegacystring--将Unicode的宽字符串转换为传统的编码方式

ProLegacystringToString--将传统编码方式的多字节字符串转换为Unicode的多字节字符串

ProLegacystringToWstring--将传统编码方式的多字节字符串转换为Unicode的宽字符串

2、字符串转换自定义函数(适用于WCHAR未设定为内定字符类型)

//wchar类型转换为CString类型输出

 wcharToCString(wchar_t* source,CString&柱激 strOut) 

     char c[MAX_PATH]; 

    ProBoolean was_used; 

    ProError status = ProWstringToLegacystring(c,260,source,PRO_VALUE_UNUSED,'^',&was_used); 

     strOut.Format("%s",c);

 }

//将字符串类型转换为wchar类型 

CStringTowchar(CString strSource,wchar_t* pout)

 { 

     ProLegacystringToWstring(pout,260,strSource.GetBuffer(strSource.GetLength()),

       strSource.GetLength()); 

}

//字符串转换为double类型

double  CStringToDouble(CString str)

 { 

      double d=atof(str.GetBuffer(str.GetLength())); 

      return d; 

}

//字符串转换为int类型 int  CStringToInt(CString str)

 { 

       int i=atoi(str.GetBuffer(str.GetLength())); return i;

 }

3、字符串比较函数番娃

//返回值为0表明两个字符串相同

int ProUtilStrcmp(char *s,char *t)//比较char*字符串

{

int i = 0;

while( toupper(s[i]) == toupper(t[i]))

{

if( s[i++] == '\0' )

return(0);

}

return(s[i] - t[i]);

}

int ProUtilWstrCmp(wchar_t *ws1,wchar_t *ws2)//比较wchar字符串

{

int len1, len2;

char* us1;

char* us2;

int result;

ProWstringLengthGet (ws1, &len1);

ProWstringLengthGet (ws2, &len2);

/* Allow extra length for wchar_t -> char conversion */

us1 = (char*) calloc ((4*len1+1), sizeof (char));

us2 = (char*) calloc ((4*len2+1), sizeof (char));

ProWstringToString (us1, ws1);

ProWstringToString (us2, ws2);

result = ProUtilStrcmp (us1, us2);

free (us1);

free (us2);

return (result);

}

4、杂类转换函数

(1)CString转Int

CString szTemp="123456";

int nTemp=atoi((LPTSTR)(LPCTSTR)szTemp);

(2)CString转Double

double  dTemp=atof((LPTSTR)(LPCTSTR)szTemp);

(3)char reBuff[10] = {0};

RegQueryValueEx(hKey,"Number",0,&dwtype,(BYTE*)reBuff,&sizeBuff) ;获取键值后,得到的数据需要转换为10进制,需要这样操作。

int count=(long)*(short *)reBuff;

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢