1.字符串的连接;
2.字符串数组的赋值;
3.LPWSTR,BSTR中字符串(含中文)的显示;
4.数据转换;
//本文内容: 1.字符串的连接; 2.字符串数组的赋值; 3.LPWSTR,BSTR中字符串(含中文)的显示;4.数据转换;
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include "iostream.h"
#include "comutil.h"
#include <comdef.h>
#pragma comment( lib, "comsupp.lib" )
#define LEN 100
//字符串的连接
BOOL getValue()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
char buffer2[100];
_bstr_t s,s2;
s = L"S7:[S7 connection_1]DB90,X" ;
float f=0.0f;
char *p,*p2;
//p=(char *)malloc(100);
p=new char[100];
p2="1234";
strcpy(p,"S7:[S7 connection_1]DB90,X");
//p="S7:[S7 connection_1]DB90,X" ;
BOOL Flag=TRUE;
for(int j1=0;j1<68;j1++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
sprintf( buffer2, "%0.1f",f);
//cout<<"buffer2="<<buffer2<<endl;
LPWSTR lps;
if(Flag)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
s2=s+buffer2;
//cout<<endl<<s+buffer2<<endl;
//cout<<endl<<s2<<endl;
lps=::SysAllocString(s2);
//cout<<" lps="<<lps<<endl;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
p2=buffer2;
strcpy(p,"S7:[S7 connection_1]DB90,X");
![](/Images/OutliningIndicators/InBlock.gif)
// char *p3="123";
// char *p23="123";
// int *i=NULL;
// int j=100;
// i=&j;
// cout<<"i="<<i<<endl;
// cout<<"p3="<<p3<<endl;
// if(p3==p23)
// // if("aa"=="aa")
// {
// cout<<"true";
// }
// else
// {
// cout<<"false";
// }
strcat(p,p2);
lps=_com_util::ConvertStringToBSTR(p) ;
}
//cout<<"编号: "<<j1<<" 地址连接符: ";
wprintf(L"%3d. %s\n", j1,lps);
f+=0.1f;
}
return true;
}
![](/Images/OutliningIndicators/None.gif)
//应用字符串连接
int printstr()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
char str[]="ABCDE";
char *string="ABCDE";
string =(char *)malloc(LEN);
strcpy(string,"123456");
cout << endl;
cout <<"字符串输出str[]="<<str<<endl;
cout <<"字符串输出*string="<<string<<endl;
char *AppendStr;
AppendStr="abc";
for(int i = 0; i< 5; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
AppendStr=str;//"456";
strcat(string ,AppendStr);
![](/Images/OutliningIndicators/InBlock.gif)
//strcat(string, "a");
}
printf("%s\n", string);
free(string);
getchar();
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
return 1;
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
int main(void)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
//printstr();
//system("PAUSE");
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
if(getValue())
cout<<"赋值成功"<<endl;
system("PAUSE");
![](/Images/OutliningIndicators/InBlock.gif)
//通过sprintf函数进行数据类型转换
char buffer2[200];
char c = '1';
int i = 35;
long j = 1000;
float f = 1.7320534f;
sprintf( buffer2, "%c",c);
sprintf( buffer2, "%d",i);
sprintf( buffer2, "%d",j);
//字符串的相加
_bstr_t s,s2;
s = L"S7:[S7 connection_1]DB90,X" ;
f=0.0f;
for(int j1=0;j1<100;j1++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
f+=0.1f;
sprintf( buffer2, "%0.1f",f);
cout<<"buffer2="<<buffer2<<endl;
s2=s+buffer2;
//cout<<endl<<s+buffer2<<endl;
cout<<endl<<s2<<endl;
BSTR bs=::SysAllocString(s2);
LPWSTR lps=::SysAllocString(s2);
cout<<"bs="<<bs<<" lps="<<lps<<endl;
wprintf(L"%s\n", bs); //显示赋值结果
wprintf(L"%s\n", lps);
}
wchar_t *wsir;//#include<comdef.h>
_bstr_t str=L"How are you,北京!";
wsir=str;
wprintf(L"%s\n", wsir);
![](/Images/OutliningIndicators/InBlock.gif)
//中文字符的显示
//说明:Converts a sequence of multibyte characters to a corresponding sequence of wide characters.
//原型:size_t mbstowcs( wchar_t* wcstr, const char* mbstr, size_t count );
//参数:Parameters
// wcstr
// The address of a sequence of wide characters.
// mbstr
// The address of a sequence of multibyte characters.
// count
// The number of multibyte characters to convert.
char* a = "你好,北京";
wchar_t *b = new wchar_t[3];
mbstowcs(b, a, sizeof(a)+1);
puts(a);
wprintf(L"%s\n", b);
system("PAUSE"); //暂停程序的执行
LPWSTR lps=L"success OK";
cout<<"lps="<<lps<<endl;
cout<<"*string2"<<endl;
char *string2[10];
int ii=0;
![](/Images/OutliningIndicators/InBlock.gif)
//字符串数组赋值
cout<<endl;
for( ii=0;ii<10;ii++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
int i=ii;
string2[i]=new char;
itoa(i,string2[i],10);
}
cout<<endl;
for(ii=0;ii<10;ii++)
cout<<string2[ii]<<endl;
![](/Images/OutliningIndicators/InBlock.gif)
//把双精度数转换成一个字符串。
//char *ecvt(double value,int count,int *dec,int *sign);
//函数 需要的头文件 兼容性
//ecvt <stdlib.h> Win 95,Win NT
char *string;
double value;
int dec=2, sign=0;
int ndig = 1;
//clrscr();
value = -9.876;
string = ecvt(value, ndig, &dec, &sign);
printf("string = %s dec = %d sign = %d\n", string, dec, sign);
value = -123.45;
ndig= 15;
string = ecvt(value,ndig,&dec,&sign);
printf("string = %s dec = %d sign = %d\n",
string, dec, sign);
int decimal, sign2=0;
char *buffer;
![](/Images/OutliningIndicators/InBlock.gif)
//fcvt是将浮点数转换为字符串,它不属于标准的C运行时库,仅仅是VC提供的,
//因此把其命名为_fcvt .
float source = 0.123456f;
buffer = _fcvt( source, 2, &decimal, &sign2 );
//printf("%s",buffer);
cout<<endl<<endl;
cout<<buffer<<endl;
cout<<source<<" "<<decimal<<" "<<sign2<<endl;
//运行结果:source: 3.1415926535 buffer: \\\'31415927\\\' decimal: 1 sign: 0
//decimal表示小数点的位置,sign表示符号:0为正数,1为负数
![](/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](/Images/OutliningIndicators/None.gif)
|