如何从char16_t字符串文字中读取双精度字?

问题描述 投票:2回答:2

我如何使用标准的C ++ 11功能从char16_t字符串(char16_t *)中读取双精度字?

我尝试过:

#include <iostream>
#include <sstream>
#include <string>

double char16_to_double( const char16_t* s )
{
  double n = 0;
  std::basic_stringstream<char16_t> ss( s );
  ss >> n;
  return n;
}

int main()
{
  try {
    double n = char16_to_double( u"0.1" );
    std::cout << "Value of n: " << n << std::endl;
  } catch( ... ) {
    std::cout << "Exception!" << std::endl;   
  }
  return 0;
}

但是,当我使用g ++-7进行编译时,它将在https://cpp.sh中引发异常,或者导致0而不是0.1。正确的方法是什么?

c++ char
2个回答
1
投票

[我发现的一种可能的解决方案是通过std::wstring_convert将char16_t(采用UTF16编码)转换为char字符串:


0
投票

您可以只转换**const char16_t*** to const char*并使用]中的atof()>

所以您的函数将如下所示:

© www.soinside.com 2019 - 2024. All rights reserved.