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
;
}
查看全文
相关阅读:
举重若轻是一种大气的生活态度
论自我发展与自我职场生存
ASP.Net与IIS原理粗浅的理解
Net反射效率(转载)
MVC技术
单件模式 多线程
公司的机票返利项目总结
JS调用google地图
System.Runtime.Serialization报错查找
信息采集
原文地址:https://www.cnblogs.com/furenjun/p/vcstring.html
最新文章
数据库命令行操作入口
BSD
.c与.cpp文件的一点区别
C与C++中的异常处理(摘)
数组与指针的区别
打开Win2000的自动补齐功能
不错的ppt模板网站
定义一个宏,求出给定结构中给定成员的偏移量
GCC LD_LIBRARY_PATH 和 LIBRARY_PATH
lua 加载C动态库
热门文章
快速排序 Gnu glibc qsort
CMake VS2010 编译运行Ogre Sample
windows cocos2dx 集成luasocket模块
基于cocos2dx的游戏可以完全使用lua开发吗?
在cocos2dx中调试lua
快速排序
我们应该有一个属于我们自己的游戏 && 我是一只单飞的鸟
快速排序 Gnu glibc qsort_r
Efective C++(2nd)学习心得
MySoft Journey 随笔心得
Copyright © 2011-2022 走看看