[英]Unsigned char to char* and int in C?
Working on some encryption that requires unsigned char's in the functions, but want to convert to a char for use after it's been decrypted. 处理一些需要在函数中使用unsigned char的加密,但希望在解密后转换为char以供使用。 So, I have:
所以我有:
unsigned char plaintext[16];
char *plainchar;
int plainint;
... Code now populates plaintext with data that happens to all be plain text
Now at this point, let's say plaintext is actually a data string of "0123456789". 现在在这一点上,让我们说明文实际上是一个“0123456789”的数据字符串。 How can I get the value of plaintext into plainchar as "012456789", and at the same time plainint as 123456789?
如何将明文的值作为“012456789”获取为plainchar,同时将plainint作为123456789?
-- Edit -- - 编辑 -
Doing this when plaintext is equal to "AAAAAAAAAA105450": 当明文等于“AAAAAAAAAA105450”时执行此操作:
unsigned char plaintext[16];
char *plainchar;
int plainint;
... Code now populates plaintext with data that happens to all be plain text
plainchar = (char*)plaintext;
Makes plainchar equal to "AAAAAAAAAA105450╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠┤∙7" with a sizeof = 51. The encryption code is the rijndael example code, so it should be working fine. 使plainchar等于“AAAAAAAAAA105450╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠┤∙7”,sizeof = 51.加密代码是rijndael示例代码,所以它应该工作正常。
Thanks, Ben 谢谢,本
Your plain text string is not terminated. 您的纯文本字符串未终止。 All strings must have an extra character that tells the end of the string.
所有字符串都必须有一个额外的字符,告诉字符串的结尾。 This character is
'\\0'
. 这个字符是
'\\0'
。
Declare the plaintext
variable as 将
plaintext
变量声明为
unsigned char plaintext[17];
and after you are done with the decryption add this 完成解密后添加此项
plaintext[last_pos] = '\0';
Change last_pos
to the last position of the decrypted text, default to 16
(last index of the array). 将
last_pos
更改为解密文本的最后位置,默认为16
(数组的最后一个索引)。
I think its simply 我认为这很简单
plainchar = (char*)plaintext;
sscanf( plainchar, "%d", &plainint );
for unsigned char to char* 对于unsigned char to char *
plainchar = (char*)plaintext;
for unsigned to int 对于unsigned to int
sscanf( plainchar, "%d", &plainint );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.