字符串类型的转换

Conversion of String-Types

C++ 中有很多字符串类型:WideString、UnicodeString、String、wstring、string、AnsiString、Variant

在我的代码中有很多转换,例如

WideString s1 = UnicodeString ( wstring(s2.str().c_str()).c_str()).c_str();

一言以蔽之:令人困惑!

有没有一种简单的方法可以用一个助手来处理所有的字符串转换-class 再也不用考虑如何将一种字符串类型转换为另一种字符串类型,例如:

s1 = sc(s2);    // sc = string-converter

sc(s1,s2);      // s1 = convert to, s2 = convert from

使用所有可能的转换创建您自己的 template class

在您的模板中,您应该使用类型安全转换,例如 static_cast

那就只有一次糊涂了...

编辑

可能是这样的:

template <typename T>
T convert(T x)
{
  T value;
  value = static_cast<T>(x);
  return value;
}

(我没有输入实现,因为这些是我不知道的变量类型。这是我尝试这样做的方式)

我主要是老式的 C89-C99 用户,因此我可以建议使用一些宏。当然,正如已经建议的那样,模板将 更优雅和安全(因为将被编译器跟踪错误),但只是让你的武器选择。

#define CONV_TO_WSTRING(s1) ( wstring(s1.str().c_str()).c_str()).c_str()

例如。

那么在使用中你有:

s1 = CONV_TO_WSTRING(s2);

创建一个包装器 class 来处理所有可能的转换。您可以使用 模板 class 但请注意,您必须制作额外的模板 专业化,因为大多数这些转换不能用 通用方式。例如,您可以轻松地将 WideString 转换为 UnicodeString 使用转换,但您不能简单地将 wstring 转换为 string。对于类型 可以转换,可以使用通用部分:

template <class TypeFrom, class TypeTo>
TypeTo convert(const TypeFrom &from)
{
    return static_cast<TypeTo>(from);
}

但这不适用于 wstringstring 的转换。你需要一个模板 专业化:

template <>
std::string convert<std::wstring, std::string>(const std::wstring &from)
{
    return std::string(from.begin(), from.end());
}

不可能在此处列出所有组合,您必须注意这一点。这里 link 可以帮助进行一些转换:

https://msdn.microsoft.com/en-us/library/ms235631.aspx

经过一些实验我决定使用重载函数!

如果我使用模板,我的代码如下所示:

string  s = convert<wstring,string> (wstring(L"hello"));

问题是我必须编写要转换的类型。我为什么要那么做?编译器知道类型!

如果我使用重载函数

std::string convert(const std::wstring &from)
{
  return std::string(from.begin(), from.end());
}

std::wstring convert(const std::string &from)
{
  return std::wstring(from.begin(), from.end());
}

我的代码看起来是这样的:

string str2 = convert(wstring(L"hello"));

这就是我喜欢的方式:永远不要重复自己。偷懒。尽可能简单。 ^^

这是我目前对转换问题的解决方案

std::string convert1(const std::wstring &from) {
  return std::string(from.begin(), from.end()); }

std::string convert1(const UnicodeString &from) {        // String = UnicodeString
  wstring strTemp = from.c_str();
  return std::string(strTemp.begin(), strTemp.end()); }

std::string convert1(const WideString &from) {
  wstring strTemp = from.c_bstr();
  return std::string(strTemp.begin(), strTemp.end()); }


std::wstring convert2(const std::string &from) {
  return std::wstring(from.begin(), from.end()); }

std::wstring convert2(const UnicodeString &from) {
  wstring strTemp = from.c_str();
  return std::wstring(strTemp.begin(), strTemp.end()); }

std::wstring convert2(const WideString &from) {
  wstring strTemp = from.c_bstr();
  return std::wstring(strTemp.begin(), strTemp.end()); }


UnicodeString convert3(const std::string &from) {
  wstring strTemp(from.begin(), from.end());
  return UnicodeString(strTemp.c_str()); }

UnicodeString convert3(const std::wstring &from) {
  return UnicodeString(from.c_str()); }

UnicodeString convert3(const WideString &from) {
  return UnicodeString(from.c_bstr()); }


WideString convert4(const std::string &from) {
  wstring strTemp(from.begin(), from.end());
  return WideString(strTemp.c_str()); }

WideString convert4(const std::wstring &from) {
  return WideString(from.c_str()); }

WideString convert4(const UnicodeString &from) {
  return WideString(from.c_str()); }



wchar_t* convert5(const std::string &from) {
  static wstring strTemp;
  strTemp.clear();
  strTemp.assign(from.begin(), from.end());
  return (wchar_t*) strTemp.c_str(); }

wchar_t* convert5(const std::wstring &from) {
  return (wchar_t*) from.c_str(); }

wchar_t* convert5(const UnicodeString &from) {
  return from.c_str(); }

wchar_t* convert5(const WideString &from) {
  return from.c_bstr(); }

用法:

  std::string   s1  = convert1 (wstring       (L"Hallo1" ));
  std::string   s2  = convert1 (UnicodeString (L"Hallo2" ));
  std::string   s3  = convert1 (WideString    (L"Hallo3" ));
  std::wstring  s4  = convert2 (std::string   ( "Hallo4" ));
  std::wstring  s5  = convert2 (UnicodeString (L"Hallo5" ));
  std::wstring  s6  = convert2 (WideString    (L"Hallo6" ));
  UnicodeString s7  = convert3 (std::string   ( "Hallo7" ));
  UnicodeString s8  = convert3 (std::wstring  (L"Hallo8" ));
  UnicodeString s9  = convert3 (WideString    (L"Hallo9" ));
  WideString    s10 = convert4 (std::string   ( "Hallo10"));
  WideString    s11 = convert4 (std::wstring  (L"Hallo11"));
  WideString    s12 = convert4 (UnicodeString (L"Hallo12"));

  Memo->Lines->Add(convert5(s1));
  Memo->Lines->Add(convert5(s2));
  Memo->Lines->Add(convert5(s3));
  Memo->Lines->Add(convert5(s4));
  Memo->Lines->Add(convert5(s5));
  Memo->Lines->Add(convert5(s6));
  Memo->Lines->Add(convert5(s7));
  Memo->Lines->Add(convert5(s8));
  Memo->Lines->Add(convert5(s9));
  Memo->Lines->Add(convert5(s10));
  Memo->Lines->Add(convert5(s11));
  Memo->Lines->Add(convert5(s12));

结果:

Hallo1
Hallo2
Hallo3
Hallo4
Hallo5
Hallo6
Hallo7
Hallo8
Hallo9
Hallo10
Hallo11
Hallo12