让萨皮说一串
Getting Sapi to say a string
本文关键字:一串 更新时间:2023-10-16
我有一个程序,要求人们将他们想要的文本翻译成Al Bhed,这只是一个密码,字母在其中移动,并由SAPI说出。字符串翻译得很好,但这段代码:
hr = pVoice->Speak(sTranslated, 0, NULL);
将不起作用,因为它说"不存在从‘std::string’到‘const WCHAR*’的合适的转换函数。我只想让声音说出翻译后的字符串。我该怎么做?
首先需要将使用类型char
的std::string
内容转换为使用类型wchar_t
的std::wstring
。这是因为ISpVoice::Speak()
函数要求第一个参数的类型为LPCWSTR
,IOW是"指向宽字符串的常量指针"。以下功能可能对您有所帮助。
inline std::wstring s2w(const std::string &s, const std::locale &loc = std::locale())
{
typedef std::ctype<wchar_t> wchar_facet;
std::wstring return_value;
if (s.empty())
{
return return_value;
}
if (std::has_facet<wchar_facet>(loc))
{
std::vector<wchar_t> to(s.size() + 2, 0);
std::vector<wchar_t>::pointer toPtr = &to[0];
const wchar_facet &facet = std::use_facet<wchar_facet>(loc);
if (0 != facet.widen(s.c_str(), s.c_str() + s.size(), toPtr))
{
return_value = to.data();
}
}
return return_value;
}
然后将代码行更改为以下内容。
hr = pVoice->Speak(s2w(sTranslated).c_str(), 0, NULL);
c_str()
方法返回一个指向std::wstring
对象的"等价C字符串"的指针。IOW,它返回一个指向以null结尾的宽字符串的指针。
相关文章:
- C++为一串单词添加空格
- 为什么Visual Studio正在清除我的一串反斜杠
- 如何将内存中的位转换为一串和零?
- 如何返回一串字母数字字符中的最大值(例如-1A003B3)?
- 创建一个程序,该程序将一串数字作为输入,并输出偶数和奇数的总和
- C++ 如何对一串数字求平均值
- 从网络数据包复制一串 wchar 时访问冲突
- C - 将文本文件读为一串单词,然后将字符串分为向量
- 我如何在一串随机生成的字母I C 中查找一个单词
- 从一串数字C++创建一个数字序列
- 将一串十六进制存储到字符中
- 在另一个字符串中查找一串数字
- 将一串数字分解为26以下数字的计数方法
- 把一串单词解析成一组单词
- 将一串数字转换为整数向量 c++
- 将一串单词视为数组 (C++)
- 使用算术记数法横向排列一串字符
- 我有编译器的情况。当我运行代码来去除一串特殊字符时,它在一个编译器中运行,但在另一个编译器中不运行?
- 将一串数字转换为任何形式的整数
- 如何压缩一串数字