zoukankan
html css js c++ java
VC中的字符串操作
//
本文内容: 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()
{
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
++
)
{
sprintf( buffer2,
"
%0.1f
"
,f);
//
cout<<"buffer2="<<buffer2<<endl;
LPWSTR lps;
if
(Flag)
{
s2
=
s
+
buffer2;
//
cout<<endl<<s+buffer2<<endl;
//
cout<<endl<<s2<<endl;
lps
=
::SysAllocString(s2);
//
cout<<" lps="<<lps<<endl;
}
else
{
p2
=
buffer2;
strcpy(p,
"
S7:[S7 connection_1]DB90,X
"
);
//
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
;
}
//
应用字符串连接
int
printstr()
{
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
++
)
{
AppendStr
=
str;
//
"456";
strcat(
string
,AppendStr);
//
strcat(string, "a");
}
printf(
"
%s\n
"
,
string
);
free(
string
);
getchar();
return
1
;
}
int
main(
void
)
{
//
printstr();
//
system("PAUSE");
if
(getValue())
cout
<<
"
赋值成功
"
<<
endl;
system(
"
PAUSE
"
);
//
通过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
++
)
{
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);
//
中文字符的显示
//
说明: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
;
//
字符串数组赋值
cout
<<
endl;
for
( ii
=
0
;ii
<
10
;ii
++
)
{
int
i
=
ii;
string2[i]
=
new
char
;
itoa(i,string2[i],
10
);
}
cout
<<
endl;
for
(ii
=
0
;ii
<
10
;ii
++
)
cout
<<
string2[ii]
<<
endl;
//
把双精度数转换成一个字符串。
//
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;
//
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为负数
return
0
;
}
查看全文
相关阅读:
python中关于with以及contextlib的使用
Python之Redis操作
Python操作memecache
COM组件技术名称解释
C++11-新增正则表达式
BSTR与char*、cstring、CComBSTR的转换
ATL字符宏使用以及代码测试
获取与一个磁盘的组织以及剩余空间容量有关的信息以及代码测试
关于cstring ->string-> const char * 用U2A一步转换 错误的内存问题
cstring、string、wstring、int、char*、tchar、 int、dword等相互转换代码输出测试
原文地址:https://www.cnblogs.com/furenjun/p/vcstring.html
最新文章
Eclipse 创建类时添加继承
Eclipse Failed to load D:android-sdk-windowsuild-tools27.0.3libdx.jar
Android Java访问本地方法(JNI)
Android JNI中的数据传递
laravel Blade 模板引擎
Laravel 中通过自定义分页器分页方法实现伪静态分页链接以利于 SEO
laravel 视图
laravel CSRF 保护
laravel 表单方法伪造
laravel 路由缓存
热门文章
laravel 频率限制throttle
laravel 兜底路由
laravel 路由模型绑定
laravel 资源控制器
HTML基础知识
用paramiko写堡垒机
Python操作SQLAlchemy之连表操作
Python操作MySQL之SQLAlchemy
Python操作MySQL之pymysql
Python之操作RabbitMQ
Copyright © 2011-2022 走看看