5
.NETプロジェクト内でアンマネージドC++コードをラッピングしています。このため私はchar*
に格納されたUTF8バイトにSystem::String
を変換する必要があります。.NET System :: Stringをcharに格納されたUTF8バイトに変換する*
これを行うには、これが最善の方法であるか正しい方法であるかどうかは不明です。誰かが一見して意見を述べることができたらうれしいです。
おかげで、
/デヴィッド・
// Copy into blank VisualStudio C++/CLR command line solution.
#include "stdafx.h"
#include <stdio.h>
using namespace System;
using namespace System::Text;
using namespace System::Runtime::InteropServices;
// Test for calling with char* argument.
void MyTest(const char* buffer)
{
printf_s("%s\n", buffer);
return;
}
int main()
{
// Create a UTF-8 encoding.
UTF8Encoding^ utf8 = gcnew UTF8Encoding;
// A Unicode string with two characters outside an 8-bit code range.
String^ unicodeString = L"This unicode string contains two characters with codes outside an 8-bit code range, Pi (\u03a0) and Sigma (\u03a3).";
Console::WriteLine(unicodeString);
// Encode the string.
array<Byte>^encodedBytes = utf8->GetBytes(unicodeString);
// Get pointer to unmanaged char array
int size = Marshal::SizeOf(encodedBytes[0]) * encodedBytes->Length;
IntPtr pnt = Marshal::AllocHGlobal(size);
Marshal::Copy(encodedBytes, 0, pnt, encodedBytes->Length);
// Ugly, but necessary?
char *charPnt= (char *)pnt.ToPointer();
MyTest(charPnt);
Marshal::FreeHGlobal(pnt);
}
非常にいいです、説明をありがとうございます。 –
この例では、 'pinnedBytes'がどのようにゼロターミネータを取得するのか分かりません。これを保証するいくつかの魔法がありますか?それとも読者のための運動として残されているのでしょうか? – StilesCrisis
@StilesCrisis oops、あなたが正しいです、私はOPが自分のcharポインタをprintf%sに渡していたことを見落としているに違いありません。実際には、固定されたバイトの後には通常0バイトが続きますので、とにかく動作しますが、それを保証するルールはわかりません。私は答えを調整します。 – Zarat