【发布时间】:2011-08-05 16:23:00
【问题描述】:
我的 C# 代码使用托管 C++ 包装器。要创建此 Wrapper 类型的新对象,我需要将 String 转换为 Sbyte*。 StackOverflow.com 的一些帖子讨论了如何将 String 转换为 byte[],以及将 byte[] 转换为 sbyte[],但没有将 String 转换为 sbyte*。
msdn.social.com 提供了有关如何将字节数组转换为字符串的建议:
> // convert String to Sbyte*
> string str = "The quick brown, fox jumped over the gentleman.";
>
> System.Text.ASCIIEncoding encoding = new
> System.Text.ASCIIEncoding();
>
> Byte[] bytes = encoding.GetBytes(str);
但是,“bytes”不是 sbyte* 类型。我以下将字节转换为 sbyte* 的尝试失败了:
1. Convert.ToSbyte(bytes); 2. cast: (sbyte*) bytes;
请告诉我如何将 C# 字符串转换为 sbyte*。
另外,请谈谈引入 sbyte* 的任何副作用,我认为这是不安全的代码。
谢谢, 凯文
【问题讨论】: