質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
char

charは文字データ型を指します。一文字分の文字コードの格納を想定としている型です。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

String

Stringは、ゼロ以上の文字から連続してできた文字の集合を扱うデータ型です。基本的にテキストを表すために使われます。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

2回答

1219閲覧

C++ Intel RealSense SDK Speech Recognitionについて

ths

総合スコア21

char

charは文字データ型を指します。一文字分の文字コードの格納を想定としている型です。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

String

Stringは、ゼロ以上の文字から連続してできた文字の集合を扱うデータ型です。基本的にテキストを表すために使われます。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2018/05/01 23:57

編集2018/05/02 01:22

C++で音声認識してテキストファイルに認識文字列を出力したいのです。
しかし、認識データがpxcCHAR型であるため、stringに変換できず、
テキストファイルには変数のアドレスが出力されてしまいます。
どうすればpxcCHARをstringに変換できますか?
下記に問題のプログラムと実行結果を掲載します。

【Mic.cpp】

#include "stdafx.h" #include <iostream> #include <Windows.h> #include <fstream> #include <string> #include <stdio.h> #include <stdlib.h> #include "pxcsensemanager.h" #include "pxcspeechrecognition.h" int main(int argc, char** argv) { //Japanese shown std::locale::global(std::locale("japanese")); /* Create Session instance After, use to get instance of PXCSpeechRecognition */ PXCSession *session = PXCSession::CreateInstance(); if (session == nullptr) { wprintf_s(L"Session not created by PXCSession\n"); return 1; } /* The instance of PXCSpeechRecognition is gotten by CreateImpl method Return value is pxcStatus type, error processing in "if(sts != pxcStatus::PXC_STATUS_NO_ERROR)" */ PXCSpeechRecognition *sr = nullptr; pxcStatus sts = session->CreateImpl<PXCSpeechRecognition>(&sr); if (sts != pxcStatus::PXC_STATUS_NO_ERROR) { wprintf_s(L"Failed to create an instance of the PXCSpeechRecognition\n"); return 2; } /* Read QueryProfile and initiallize module PXCSpeechRecognition::ProfileInfo conatins the setting item such as language Set this, reflect using SetProfile */ PXCSpeechRecognition::ProfileInfo pinfo; sr->QueryProfile(0, &pinfo); pinfo.language = PXCSpeechRecognition::LanguageType::LANGUAGE_JP_JAPANESE; sts = sr->SetProfile(&pinfo); if (sts != pxcStatus::PXC_STATUS_NO_ERROR) { wprintf_s(L"Failed to Configure the Module\n"); return 3; } //Command mode or Dictation mode sts = sr->SetDictation(); if (sts != pxcStatus::PXC_STATUS_NO_ERROR) { wprintf_s(L"Failed to set the recognition mode\n"); return 4; } /* Override OnRecognition() and OnAlert() method OnRecognition() is called when got result of recognition, @Param PXCSpeechRecognition::RecognitionData OnAlert() is called when happend caution, @Param PXCSpeechRecognition::AlertData In the AlertData has information of speak start-end and sound small */ /*音声認識部分*/ class MyHandler : public PXCSpeechRecognition::Handler { public: std::string path = "C:/Users/user/Dropbox/workspace/G5 Graduate Study/smartspeaker/text/input.txt"; virtual void PXCAPI OnRecognition(const PXCSpeechRecognition::RecognitionData *data) { wprintf_s(L"Output %s\n", data->scores[0].sentence); std::ofstream ofs(path); ofs << data->scores[0].sentence; //2018.05.02 問題の箇所 ofs.close(); } virtual void PXCAPI OnAlert(const PXCSpeechRecognition::AlertData *data) { if (data->label == PXCSpeechRecognition::ALERT_SPEECH_BEGIN) { wprintf_s(L"Alert: SPEECH_BEGIN\n"); } else if (data->label == PXCSpeechRecognition::ALERT_SPEECH_END) { wprintf_s(L"Alert: SPEECH_END\n"); } else if (data->label == PXCSpeechRecognition::ALERT_VOLUME_LOW) { wprintf_s(L"Alert: VOLUME_LOW\n"); } } }; MyHandler handler; sts = sr->StartRec(nullptr, &handler); if (sts != pxcStatus::PXC_STATUS_NO_ERROR) { wprintf_s(L"Failed to start the handler\n"); return 5; } while (true) { if (GetAsyncKeyState(VK_ESCAPE)) break; } sr->StopRec(); return 0; }

【実行結果】

Alert: SPEECH_BEGIN Alert: SPEECH_END Output おはようございます

【input.txt】

06,248,108

【補足】
問題の部分を変更しました

virtual void PXCAPI OnRecognition(const PXCSpeechRecognition::RecognitionData *data) { wprintf_s(L"Output %s\n", data->scores[0].sentence); //std::ofstream ofs(path); wstring_convert<codecvt_utf8<wchar_t>, wchar_t> cv; wstring wstr = wstring(data->scores[0].sentence);   string str = cv.to_bytes(wstr); cout << str << endl; //ofs << wstr//2018.05.02 問題の箇所 //ofs.close(); }

コンソールで実行すると、strが文字化けしています。
日本語のため、まだ追加処理が必要なのでしょうか?

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

ざっと見た感じ、native/include/pxcdefs.h に以下の定義がありました。

C++

1typedef wchar_t pxcCHAR;

何も考えずにstringにするなら、wstringを使う方がスムーズです。

C++

1std::wstring wstr(pxcchar_string);

wstringとstringの相互変換を行いたい場合は、それはまた別の話になるかと思います。

投稿2018/05/02 00:48

kazto

総合スコア7196

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ths

2018/05/02 01:39

回答ありがとうございました。 strconv.hをインポートしwide_to_ansi(data->scores[0].sentence)と記述することで、解決しました!
guest

0

WindowsではpxcCHARwchar_t型(UTF-16)のことなので、Win32APIなどでUTF-8に変換してから書き出しましょう。

変換している例:
そろそろWindowsでUTF-16とShift-JISの変換方法をC++erらしくまとめようか
WideCharToMultiByteの第一引数にCP_UTF8を渡すように書き換えればよい

投稿2018/05/02 00:38

編集2018/05/02 01:21
yumetodo

総合スコア5850

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ths

2018/05/02 01:40

回答ありがとうございました。 strconv.hをインポートしwide_to_ansi(data->scores[0].sentence)と記述することで、解決しました! まさかコード一行で解決できるとは思いませんでした。
hmmm

2018/05/02 16:58

リンク先の記事を見ましたが若干気になる箇所がありました。 MultiByteToWideCharに-1を指定しているので戻り値はNull終端文字を考慮した長さです。 std::wstring re(len * 2 + 2, L'\0'); ですので、上記の+2はあまり意味がありません。 また、932とコードページを直接指定したあとに、CP_ACPでMultiByteToWideCharを実行しているので、日本語環境以外で正しく変換できません。
hmmm

2018/05/02 17:04

追記。見逃していました。 std::wstring re(len * 2 + 2, L'\0'); lenは文字数なので2倍する必要もないですね。
yumetodo

2018/05/03 00:05

あやっ、なんでそんなことしたんだろうな・・・、あとで直しておきます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問