字符数组到无符号字符 *
char array to unsigned char *
所以我有这个函数,它接收一个指针:
int myfunc( const char *token, unsigned char *plaintext )
我做我的事情,最终得到一个字符数组:
unsigned char my_plaintext[1024];
现在我需要将该指针(纯文本(设置为my_plaintext中的内容。 我已经尝试了许多不同的方法,但我还没有弄清楚这个......
这部分在 cpp 文件中,我什至尝试过:
std::string tmpstr( my_plaintext );
但这又回来了:
token_crypto.cpp:131:13: error: invalid conversion from 'char*' to 'unsigned char*' [-fpermissive]
my_plaintext
^~~~~~~~~~~~
和
std::string tmpstr( (char *)my_plaintext );
'�5�B'
这确实可以编译,但内容都是错误的:
编辑:
my_plaintext的内容很好:
int myfunc( const char *token, unsigned char *plaintext ) {
unsigned char my_plaintext[1024];
... some processing stuff (specifically gcm_decrypt) to which is pass my_plaintext ...
cout << my_plaintext
// prints: hello:world
但是,我尝试将明文的内容设置为my_plaintext编译失败或打印一些奇怪的字符的任何内容。
如果您知道plaintext
已经指向一个 1024 长(或更长(的数组,那么您可以使用memmove()
:
int myfunc( const char *token, unsigned char *plaintext )
{
unsigned char my_plaintext[1024];
/* ... fill in my_plaintext here ... */
memmove(plaintext, my_plaintext, 1024);
/* ... rest of function ... */
}
请注意,要memmove
的参数是确定的,然后是源,而不是相反。
由函数的调用方来确保它们传入的指针指向至少 1024 个字节。
在这种情况下,您可以改用memcpy()
,但通常使用memmove()
是一种很好的做法。
C++字符串构造函数不接受无符号字符 *。请参阅此处的C++参考:
http://www.cplusplus.com/reference/string/string/string/
您需要将无符号字符数组强制转换为字符数组。在此处查看操作方法:
如何在C++中将无符号字符*转换为标准::字符串?
相关文章:
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- C++中无符号字符溢出
- 使用无符号字符数组有效存储内存
- 在 std::无符号字符的向量处存储 int 的十六进制表示形式
- 如何在保持其值的同时将 c++ 无符号字符变量转换为 char 变量
- 从 std::vector<无符号字符>切片中提取 int?
- 将无符号字符的向量存储在数组中会给我 std::bad_alloc
- 使用 fopen 打开 .pak 文件并使该文件应用于 const 无符号字符* (C++)
- 库特无符号字符
- 寻找一种更好的方法来表示无符号字符数组
- C++ 无符号字符 *{varName};-> C#
- 如何返回/复制unique_ptr<无符号字符[]>的值?
- 将浮点数转换为无符号字符数组并打印出来
- 在函数中返回无符号字符数组,但不返回指针
- 如何在 C/C++ 中将无符号字符*转换为无符号字符数组?
- 如何返回实际值(在我的例子中是无符号字符数组)而不是来自 C++ 函数的指针?
- 如何将带有十六进制值的 std::string 转换为 std::vector<无符号字符>
- 无符号字符打印其 ASCII 值
- 字符数组到无符号字符 *
- 是否可以在 c++ 中将两位数保存在无符号字符中