如何将CString转换成wstring

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将CString转换成wstring相关的知识,希望对你有一定的参考价值。

首先你要确认你的选择的开发环境,是基于unicode的,还是基于多字节的啊。如果是基于多字节的话,你需要进行ascii和宽字节直接的转换(基于MultiByteToWideChar这个函数),如果是基于unicode模式下的,CString类本身提供宏定义进行切换,就是说你可以直接对CString 和 wstring进行相互赋值。 参考技术A 可以用sscanf
如果是宽字符utf,请使用wtof

Example
This program shows how numbers stored as strings can be converted to numeric values using the atof function.

Copy Code
// crt_atof.c
//
// This program shows how numbers stored as
// strings can be converted to numeric
// values using the atof function.

#include <stdlib.h>
#include <stdio.h>

int main( void )

char *str = NULL;
double value = 0;

// An example of the atof function
// using leading and training spaces.
str = " 3336402735171707160320 ";
value = atof( str );
printf( "Function: atof( \"%s\" ) = %e\n", str, value );

// Another example of the atof function
// using the 'd' exponential formatting keyword.
str = "3.1412764583d210";
value = atof( str );
printf( "Function: atof( \"%s\" ) = %e\n", str, value );

// An example of the atof function
// using the 'e' exponential formatting keyword.
str = " -2309.12E-15";
value = atof( str );
printf( "Function: atof( \"%s\" ) = %e\n", str, value );



Output

Function: atof( " 3336402735171707160320 " ) = 3.336403e+021
Function: atof( "3.1412764583d210" ) = 3.141276e+210
Function: atof( " -2309.12E-15" ) = -2.309120e-012

如何将 MFC CString 与 boost 字符串算法库一起使用

【中文标题】如何将 MFC CString 与 boost 字符串算法库一起使用【英文标题】:How do I use MFC CString with the boost string algorithm library 【发布时间】:2013-07-04 09:56:52 【问题描述】:

初步说明:string_algo 与 std::wstring 配合得很好,当然,如果需要算法,我可以(并且确实)首先将 CString 对象转换为 std::wstring来自 string_algo。如果我可以直接放入 CString 对象,那就太好了——与现有代码的集成会容易得多!

我想做什么:

CString in = ...;
const CString out = boost::replace_last_copy(in, L"SEARCH", L"REPLACE");

作为suggested here,我尝试另外包含boost/range/mfc.hpp 以拥有Boost.Range adapted to MFC。 (虽然我不太明白this header 看起来它似乎对CString 没有任何作用,只对集合类。)

使用 Visual Studio 2005 提升 1.44.0

测试代码如下:

#include "stdafx.h"
#include <iostream>
#include <boost/range/mfc.hpp>
#include <boost/algorithm/string.hpp>

void f(CString const& input) 
    CString out = boost::replace_last_copy(input, L"FROM", L"TO");
    std::wcout << out << "\n";

我得到的错误如下所示:

1>c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format_store.hpp(77) : error C2039: 'type' : is not a member of 'boost::range_const_iterator<C>'
1>        with
1>        [
1>            C=const CString
1>        ]
1>        c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format.hpp(139) : see reference to function template instantiation 'bool boost::algorithm::detail::check_find_result<const InputT,const FindResultT>(InputT &,FindResultT &)' being compiled
1>        with
1>        [
1>            InputT=CString,
1>            FindResultT=boost::iterator_range<const wchar_t *>
1>        ]
1>        c:\programme\boost_library-1_44_0\boost\algorithm\string\find_format.hpp(113) : see reference to function template instantiation 'InputT boost::algorithm::detail::find_format_copy_impl<SequenceT,FormatterT,boost::iterator_range<IteratorT>>(const InputT &,FormatterT,const FindResultT &)' being compiled
1>        with
1>        [
1>            InputT=CString,
1>            SequenceT=CString,
1>            FormatterT=boost::algorithm::detail::const_formatF<boost::iterator_range<const wchar_t *>>,
1>            IteratorT=const wchar_t *,
1>            FindResultT=boost::iterator_range<const wchar_t *>
1>        ]
1>        c:\programme\boost_library-1_44_0\boost\algorithm\string\replace.hpp(314) : see reference to function template instantiation 'SequenceT boost::algorithm::find_format_copy<SequenceT,boost::algorithm::detail::last_finderF<SearchIteratorT,PredicateT>,boost::algorithm::detail::const_formatF<RangeT>>(const SequenceT &,FinderT,FormatterT)' being compiled
1>        with
1>        [
1>            SequenceT=CString,
1>            SearchIteratorT=const wchar_t *,
1>            PredicateT=boost::algorithm::is_equal,
1>            RangeT=boost::iterator_range<const wchar_t *>,
1>            FinderT=boost::algorithm::detail::last_finderF<const wchar_t *,boost::algorithm::is_equal>,
1>            FormatterT=boost::algorithm::detail::const_formatF<boost::iterator_range<const wchar_t *>>
1>        ]
1>        ....\mytest.cpp(10) : see reference to function template instantiation 'SequenceT boost::algorithm::replace_last_copy<CString,const wchar_t[5],const wchar_t[3]>(const SequenceT &,Range1T (&),Range2T (&))' being compiled
1>        with
1>        [
1>            SequenceT=CString,
1>            Range1T=const wchar_t [5],
1>            Range2T=const wchar_t [3]
1>        ]
1>c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format_store.hpp(78) : error C3203: 'type' : unspecialized class template can't be used as a template argument for template parameter 'IteratorT', expected a real type
1>c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format.hpp(121) : error C2780: 'void boost::algorithm::detail::insert(InputT &,InputT::iterator,const InsertT &)' : expects 3 arguments - 4 provided
1>        c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\sequence.hpp(39) : see declaration of 'boost::algorithm::detail::insert'
1>        c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format.hpp(144) : see reference to function template instantiation 'InputT boost::algorithm::detail::find_format_copy_impl2<InputT,FormatterT,FindResultT,boost::iterator_range<IteratorT>>(const InputT &,FormatterT,const FindResultT &,const FormatResultT &)' being compiled
1>        with
1>        [
1>            InputT=CString,
1>            FormatterT=boost::algorithm::detail::const_formatF<boost::iterator_range<const wchar_t *>>,
1>            FindResultT=boost::iterator_range<const wchar_t *>,
1>            IteratorT=const wchar_t *,
1>            FormatResultT=boost::iterator_range<const wchar_t *>
1>        ]
1>c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format.hpp(121) : error C2893: Failed to specialize function template 'void boost::algorithm::detail::insert(InputT &,InputT::iterator,ForwardIteratorT,ForwardIteratorT)'
1>        With the following template arguments:
1>        'CString'
1>        'const wchar_t *'
1>c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format.hpp(123) : error C2893: Failed to specialize function template 'void boost::algorithm::detail::insert(InputT &,InputT::iterator,const InsertT &)'
1>        With the following template arguments:
1>        'CString'
1>        'boost::iterator_range<IteratorT>'
1>        with
1>        [
1>            IteratorT=const wchar_t *
1>        ]
1>c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format.hpp(123) : error C2780: 'void boost::algorithm::detail::insert(InputT &,InputT::iterator,ForwardIteratorT,ForwardIteratorT)' : expects 4 arguments - 3 provided
1>        c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\sequence.hpp(29) : see declaration of 'boost::algorithm::detail::insert'
1>c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format.hpp(125) : error C2780: 'void boost::algorithm::detail::insert(InputT &,InputT::iterator,const InsertT &)' : expects 3 arguments - 4 provided
1>        c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\sequence.hpp(39) : see declaration of 'boost::algorithm::detail::insert'
1>c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format.hpp(125) : error C2893: Failed to specialize function template 'void boost::algorithm::detail::insert(InputT &,InputT::iterator,ForwardIteratorT,ForwardIteratorT)'
1>        With the following template arguments:
1>        'CString'
1>        'const wchar_t *'

【问题讨论】:

【参考方案1】:

尝试将CString 替换为CStringW,以防CString 映射到CStringA

确切的错误代码在标题的哪一行呢?可以尝试替换本地代码中的相同错误吗?

【讨论】:

这绝对是一个 UNICODE 版本,所以CStringW。完整的测试代码在问题中。 (取模一个大部分为空的main 如果你明确类型转换为 LPCWSTR,或者使用 GetString,或者传递一个非 const CString 对象怎么办?

以上是关于如何将CString转换成wstring的主要内容,如果未能解决你的问题,请参考以下文章

如何将string转换成wstring C++

wstring如何转换成 LPCTSTR

如何将CString和:: std :: string :: std :: wstring互相转换?

mfc中,如何将CString 转换成 string

C++ MFC CString怎么转换成Double

如何将char数组转换为CString