- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我继承了一个 C++ DLL 及其相关头文件,其函数声明如下
int func1(int i);
int func2(char* s);
我还继承了一个引用VC++引用类,它包装了上面的DLL以便在C#环境中使用
public ref class MyWrapperClass
{
int func1_w(int i)
{
return func1(i);
}
int func2_w(char* s)
{
return func2(s);
}
}
在我的 C# 应用程序中,我可以使用 func1_w(int i),但我不明白如何将字符串传递给 func2_w(sbyte* s):我收到一个错误,提示我无法获取指向sbyte。我将 C# 项目设置为不安全,并将该函数声明为不安全。
如何将 sbyte* 参数传递给函数 func2_w?
最佳答案
正如我在评论中所说,这可能是我见过的最愚蠢的包装器。您必须手动分配 ansi 字符串。两种方式:
string str = "Hello world";
IntPtr ptr = IntPtr.Zero;
try
{
ptr = Marshal.StringToHGlobalAnsi(str);
// Pass the string, like:
mwc.func2_w((sbyte*)ptr);
}
finally
{
Marshal.FreeHGlobal(ptr);
}
另一种方法,使用Encoding.Default
(但请注意终止\0
的特殊处理)
string str = "Hello world";
// Space for terminating \0
byte[] bytes = new byte[Encoding.Default.GetByteCount(str) + 1];
Encoding.Default.GetBytes(str, 0, str.Length, bytes, 0);
fixed (byte* b = bytes)
{
sbyte* b2 = (sbyte*)b;
// Pass the string, like:
mwc.func2_w(b2);
}
关于c# - 如何将 C# 类似字符串的变量传递给 sbyte* 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51207422/
给定这段代码 [TestMethod] public void SByte() { int integerValue = -129; sbyte sbyteValueFromInt = (sbyt
如何将字节值 (0.255) 转换为 (-128 - +127)?例如240 -> -16 该方法应该很快(可能考虑按位运算符)。 最佳答案 经典的是 var sbyte = (byte & 127)
我的 C# 代码使用托管 C++ 包装器。要创建此 Wrapper 类型的新对象,我需要将 String 转换为 Sbyte*。 StackOverflow.com 的一些帖子讨论了如何将 Strin
我正在从串行端口读取字节,但我需要将它们转换为有符号的 8 位整数 (SByte)。不幸的是,Visual Basic 中的溢出检查会阻止 Byte 值 255 在 SByte 中变为 -1。因此,本
我仍在尝试使用 Novell 的 c# 库向 Open LDAP 中的人添加 jpeg 图像: Openldap: Add jpegPhoto to inetOrgPerson Novell.Dire
我正在做一个小任务,需要我从字节数组创建 IBuffer 实例。 对于常规字节数组,System.Runtime.InteropServices.WindowsRuntime 中的 .AsByte()
我正在用 C# 编程。我有一个 sbyte 变量。假设它包含 -10,二进制为 11110110。我想将这个值的二进制表示形式存储在一个字节变量中。因此,当我将 sbyte (-10) 复制到字节时,
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 7 年前。 Improve this qu
我有一个 sbyte 类型的变量,想将内容复制到 byte。转换不是值转换,而是一点一点的复制。 例如, 如果 mySbyte in bits 是:'10101100',转换后,相应的字节变量也将包含
我不确定这是否是 .NET 错误,但我发现它真的很有趣。 不出所料,我不能这样做: sbyte[] sbytes = { 1, 2, 3 }; byte[] bytes = sbytes; // fa
我有两个字节。我如何必须通过忽略每个字节的最高有效位来组合这两个字节。 事实上两个字节是有符号字节。所以我必须忽略最高有效位并连接 7 个剩余位。 这是我的代码和简单的示例。我得到每个字节的最后 7
我尝试将数组从 byte[] 转换为 sbyte[]。 这是我的示例数组: byte[] unsigned = { 0x00, 0xFF, 0x1F, 0x8F, 0x80 }; 我已经尝试过这个:
我想将一个字节转换为一个 sbyte,而不更改任何位。 byte b = 0x84; sbyte sb = unchecked((sbyte)b); Console.WriteLine("0x" +
写着 byte[][] getImagesForFields(java.lang.String[] fieldnames) 获取给定字段的图像数组。 另一方面,只要我在使用c#构建在asp.net 2
我正在构建一个 c# 应用程序,在我的应用程序中,我加载了一个 c++/cli dll,并调用了它的函数。 我已经在我的 c++/cli 类中声明了一个值类。 public value class S
为什么 GetBytes 返回两个元素的数组而不是一个元素的数组,尽管 sbyte 的存储只需要 1 个字节。 byte[] byteArray = BitConverter.GetBytes((sb
我一直在寻找从 sbyte[] 转换为 byte[] 的几种方法 我见过最常见的做法如下 1. sbyte[] signed = { -2, -1, 0, 1, 2 }; byte[] unsigne
我需要规范化一个 8 位音频 .wav 文件。 我使用 NAudio 将文件加载到 byte[]。我成功地将其转换为 sbyte[] 并恢复了正弦位。然后我将这个数组相乘(转换为 float)以获得规
我继承了一个 C++ DLL 及其相关头文件,其函数声明如下 int func1(int i); int func2(char* s); 我还继承了一个引用VC++引用类,它包装了上面的DLL以便在C
我似乎无法找到将 sbyte 转换回原始整数值的方法。 int k = 148; sbyte t = (sbyte)k; int asd = Convert.ToInt32(t); asd 的值为-1
我是一名优秀的程序员,十分优秀!