将System::Byte的锯齿数组转换为无符号char**
convert jagged array of System::Byte to unsigned char**
我想实现一个c++ CLI函数,将System::Byte的锯齿数组转换为无符号char**。我做了这个:
unsigned char** NUANCECLR::IsItYou::convertBBtoCC(array<array<System::Byte>^>^ b)
{
unsigned char** x = NULL;
for (size_t indx = 0; indx < b->Length; indx++)
{
if (b[indx]->Length > 1)
{
pin_ptr<System::Byte> p = &b[indx][0];
unsigned char* pby = p;
char* pch = reinterpret_cast<char*>(pby);
x[indx] = reinterpret_cast<unsigned char *>(pch);
}
else
x[indx] = nullptr;
}
return x;
}
我目前无法测试,也许有人可以帮助我,告诉我是否可以,因为我需要它相对较快。谢谢你!
不行。这会以不同的方式出现在你面前:
unsigned char** NUANCECLR::IsItYou::convertBBtoCC(array<array<System::Byte>^>^ b)
{
unsigned char** x = NULL;
未分配存储。x[anything]
将无效。
for (size_t indx = 0; indx < b->Length; indx++)
{
if (b[indx]->Length > 1)
{
pin_ptr<System::Byte> p = &b[indx][0];
这个固定指针将在if块结束时超出作用域并解除固定。系统可能会再次随意移动或删除
unsigned char* pby = p;
这将获取一个指向围绕一个字节的对象waappers数组的指针,并将其赋值给一个char
数组。我不会在这里声称自己是专业人士,但我不相信如果没有很多隐藏的巫术,这将无法透明地工作。
char* pch = reinterpret_cast<char*>(pby);
这将实际工作,但因为前面的可能没有,我不希望pch
指向任何有意义的东西。
x[indx] = reinterpret_cast<unsigned char *>(pch);
如上所述,x
不指向任何存储。这是注定的。
}
else
x[indx] = nullptr;
也注定
}
return x;
仍然注定失败。
}
推荐:
- 为大小为
- 为大小为
b[indx]->Length
的char
数组分配new
的非托管存储,并复制b
的所有元素到其中,然后分配给x[indx]
。 - 返回
x
- 确保
x
和x
指向的所有数组在完成后都被删除。或者使用vector<vector<char>>
代替char**
。
b->Length
的char *
数组分配new
的非托管存储,并分配给x
相关文章:
- 将无符号char*转换为std::istream*C++
- 为什么这个函数将"const char*"转换为"void* const"而不是"const void*"
- 使用char类型将decimal转换为string,将string转换为decimal
- 如何在对<char>C++程序进行逆向工程的同时将 std::basic_string 转换为 Rust 可读值?
- 将函数参数"const char*"转换为"std::string_view"是
- 如何在保持其值的同时将 c++ 无符号字符变量转换为 char 变量
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- 如何在不强制转换每个参数的情况下删除初始值设定项列表中从 int 到 char 的缩小转换?
- C ++如何在使用"tolower"时将char转换为int
- 有没有办法在C++的赋值中将"char**"转换为"double"?
- 如何使用多次 memcpy 转换回 std::vector<unsigned char>?
- 将"uint8_t"(从套接字读取)隐式转换为"char"安全吗
- JavaCPP 错误:无法将参数"1"的"char*"转换为"Abc*
- 将传入的网络"char*"数据转换为"uint8_t"并返回的安全方法是什么?
- 转换char*时的值不同
- Boost Python 不会自动转换 char* 数据成员
- 除了转换char*之外,还有更好的方法来获得一个变量值吗?
- 如何跨平台转换char*到long long(64位)
- 用atof转换char数组中的浮点数
- C转换char[]为时间戳;