简体   繁体   English

从C ++ / CLI向C#传递UTF字符

[英]Pass a UTF character from C++/CLI to C#

How do I pass a UTF-16 char from a C++/CLI function to a .NET function? 如何将UTF-16字符从C ++ / CLI函数传递给.NET函数? What types do I use on the C++/CLI side and how do I convert it? 我在C ++ / CLI端使用什么类型,以及如何转换它?

I've currently defined the C++/CLI function as follows: 我目前已如下定义C ++ / CLI函数:

wchar_t GetCurrentTrackID();  // 'wchar_t' is the C++ unicode char equivalent to .NET's 'char'?

The .NET wrapper is defined as: .NET包装器定义为:

System::Char GetCurrentTrackID();  // here, 'char' means UTF-16 char

I'm currently using this to convert it, but when testing it I only get a null character. 我目前正在使用它进行转换,但是在测试时,我只会得到一个空字符。 How do I properly convert a unicode char code to its char equivalent for .NET? 如何将Unicode字符代码正确转换为.NET的等价char

#pragma managed

return (System::Char)player->GetCurrentTrackID();

They are directly compatible. 它们是直接兼容的。 You can assign a Char to a wchar_t and the other way around without a cast, the compiler will not emit any kind of conversion function call. 您可以将Char分配给wchar_t,反之亦然,无需强制转换,编译器将不会发出任何类型的转换函数调用。 This is true for many simple value types in C++/CLI, like Boolean vs bool, SByte vs char, Byte vs unsigned char, Int16 vs short, Int32 vs int or long, Int64 vs long long, Single vs float, Double vs double. 这对于C ++ / CLI中的许多简单值类型都是正确的,例如Boolean vs bool,SByte vs char,Byte vs unsigned char,Int16 vs short,Int32 vs int或long,Int64 vs long long,Single vs float,Double vs double。 Plus their unsigned varieties. 加上他们未签名的品种。 The compiler will treat them as aliases since they have the exact same binary representation. 编译器会将它们视为别名,因为它们具有完全相同的二进制表示形式。

But not strings or arrays, they are classes with a non-trivial implementation that doesn't match their native versions at all. 但是不是字符串或数组,它们是具有非平凡实现的类,这些实现根本与它们的本机版本不匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM