1. <tfoot id='jHiQe'></tfoot>
      <i id='jHiQe'><tr id='jHiQe'><dt id='jHiQe'><q id='jHiQe'><span id='jHiQe'><b id='jHiQe'><form id='jHiQe'><ins id='jHiQe'></ins><ul id='jHiQe'></ul><sub id='jHiQe'></sub></form><legend id='jHiQe'></legend><bdo id='jHiQe'><pre id='jHiQe'><center id='jHiQe'></center></pre></bdo></b><th id='jHiQe'></th></span></q></dt></tr></i><div id='jHiQe'><tfoot id='jHiQe'></tfoot><dl id='jHiQe'><fieldset id='jHiQe'></fieldset></dl></div>

      <legend id='jHiQe'><style id='jHiQe'><dir id='jHiQe'><q id='jHiQe'></q></dir></style></legend>
        <bdo id='jHiQe'></bdo><ul id='jHiQe'></ul>

      <small id='jHiQe'></small><noframes id='jHiQe'>

      1. .NET System::String 到存储在 char* 中的 UTF8 字节

        .NET System::String to UTF8-bytes stored in char*(.NET System::String 到存储在 char* 中的 UTF8 字节)
        <legend id='lHcOM'><style id='lHcOM'><dir id='lHcOM'><q id='lHcOM'></q></dir></style></legend>
      2. <small id='lHcOM'></small><noframes id='lHcOM'>

          <tbody id='lHcOM'></tbody>

        <tfoot id='lHcOM'></tfoot>
          <i id='lHcOM'><tr id='lHcOM'><dt id='lHcOM'><q id='lHcOM'><span id='lHcOM'><b id='lHcOM'><form id='lHcOM'><ins id='lHcOM'></ins><ul id='lHcOM'></ul><sub id='lHcOM'></sub></form><legend id='lHcOM'></legend><bdo id='lHcOM'><pre id='lHcOM'><center id='lHcOM'></center></pre></bdo></b><th id='lHcOM'></th></span></q></dt></tr></i><div id='lHcOM'><tfoot id='lHcOM'></tfoot><dl id='lHcOM'><fieldset id='lHcOM'></fieldset></dl></div>

                  <bdo id='lHcOM'></bdo><ul id='lHcOM'></ul>
                  本文介绍了.NET System::String 到存储在 char* 中的 UTF8 字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我在 .NET 项目中封装了一些非托管 C++ 代码.为此,我需要将 System::String 转换为存储在 char* 中的 UTF8 字节.

                  I am wrapping some unmanaged C++ code inside a .NET project. For this I need to convert System::String to UTF8-bytes stored in char*.

                  我不确定这是否是最好的甚至是正确的方法,如果有人可以查看并提供反馈,我将不胜感激.

                  I am unsure if this is the best or even a correct way to do this and I'd appreciate if someone could take a look and provide feedback.

                  谢谢,

                  /大卫

                  // 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
                  ", 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);
                  
                  }
                  

                  推荐答案

                  1. 无需创建编码器实例,使用静态实例即可.

                  1. You don't need to create an encoder instance, you can use the static instances.

                  如果被调用的函数不期望指向 HGlobal 堆的指针,您可以对缓冲区使用普通的 C/C++ 内存分配(new 或 malloc).

                  If the called function doesn't expect a pointer to the HGlobal heap you can just use plain C/C++ memory allocation (new or malloc) for the buffer.

                  在您的示例中,该函数不具有所有权,因此您根本不需要副本,只需固定缓冲区即可.

                  In your example the function doesn't take ownership so you don't need a copy at all, just pin the buffer.

                  类似:

                  // Encode the text as UTF8
                  array<Byte>^ encodedBytes = Encoding::UTF8->GetBytes(unicodeString);
                  
                  // prevent GC moving the bytes around while this variable is on the stack
                  pin_ptr<Byte> pinnedBytes = &encodedBytes[0];
                  
                  // Call the function, typecast from byte* -> char* is required
                  MyTest(reinterpret_cast<char*>(pinnedBytes), encodedBytes->Length);
                  

                  或者,如果您需要像大多数 C 函数(包括 OP 中的示例)一样以零结尾的字符串,那么您可能应该添加一个零字节.

                  Or if you need the string zero-terminated like most C functions (including the example in the OP) then you should probably add a zero byte.

                  // Encode the text as UTF8, making sure the array is zero terminated
                  array<Byte>^ encodedBytes = Encoding::UTF8->GetBytes(unicodeString + "");
                  
                  // prevent GC moving the bytes around while this variable is on the stack
                  pin_ptr<Byte> pinnedBytes = &encodedBytes[0];
                  
                  // Call the function, typecast from byte* -> char* is required
                  MyTest(reinterpret_cast<char*>(pinnedBytes));
                  

                  这篇关于.NET System::String 到存储在 char* 中的 UTF8 字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
                  Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
                  Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
                  How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
                  how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
                  JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)

                  <small id='fNzGV'></small><noframes id='fNzGV'>

                  <i id='fNzGV'><tr id='fNzGV'><dt id='fNzGV'><q id='fNzGV'><span id='fNzGV'><b id='fNzGV'><form id='fNzGV'><ins id='fNzGV'></ins><ul id='fNzGV'></ul><sub id='fNzGV'></sub></form><legend id='fNzGV'></legend><bdo id='fNzGV'><pre id='fNzGV'><center id='fNzGV'></center></pre></bdo></b><th id='fNzGV'></th></span></q></dt></tr></i><div id='fNzGV'><tfoot id='fNzGV'></tfoot><dl id='fNzGV'><fieldset id='fNzGV'></fieldset></dl></div>
                    <bdo id='fNzGV'></bdo><ul id='fNzGV'></ul>
                  • <tfoot id='fNzGV'></tfoot>

                    1. <legend id='fNzGV'><style id='fNzGV'><dir id='fNzGV'><q id='fNzGV'></q></dir></style></legend>
                        <tbody id='fNzGV'></tbody>