当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
stringwstring_bstr_t的互相转换
发布时间:2010/8/6 10:23:17 来源:城市学习网 编辑:ziteng
  今天在研究VC++ 使用 ADO 读取MS SQKSERVER 数据库时, 发现:
  string       ansi字符串
  wstring    unicode字符串
  _bstr_t     com字符串
  之间的转换关系,示例如下:
  // ex_02_wstring_cv_string.cpp : 定义控制台应用程序的入口点。
  //
  #include "stdafx.h"
  #include "string"
  #include "icrsint.h"
  #include "comutil.h"
  using namespace std;
  #import "C:\\Program Files\\Common Files\\System\\ADO\\msado15.dll" no_namespace rename("EOF", "EndOfFile")
  int _tmain(int argc, _TCHAR* argv[])
  {
  CoInitialize(NULL);
  wstring ws1,ws2;
  ws2 = L"春如旧,人空瘦,泪痕红浥鲛绡透。桃花落,闲池阁,山盟虽在,锦书难托。莫、莫、莫。"; [NextPage]   // 从 unicode 字符串转化为 ansi字符串
  string s = (char *) _bstr_t ( ws2.c_str() );
  // 从 ansi字符串转化为 unicode 字符串
  ws1 = ( wchar_t *) ( _bstr_t ( s.c_str( ) ));
  setlocale(LC_ALL, "chs");
  wprintf( L"原wstring串=%s\n",ws2.c_str());
  printf( "转换为string串=%s\n", s.c_str());
  wprintf( L"转换为wstring串=%s\n",ws1.c_str());
  ::CoUninitialize();
  getchar();
  return 0;
  }
  --- 结果 ---
  原wstring串=春如旧,人空瘦,泪痕红浥鲛绡透。桃花落,闲池阁,山盟虽在,锦书难托。莫、莫、莫。
  转换为string串=春如旧,人空瘦,泪痕红浥鲛绡透。桃花落,闲池阁,山盟虽在,锦书难托。莫、莫、莫。
  转换为wstring串=春如旧,人空瘦,泪痕红浥鲛绡透。桃花落,闲池阁,山盟虽在,锦书难托。莫、莫、莫。
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved