![](/img/trans.png)
[英]typecasting unsigned char and signed char to int in C
[英]Unsigned char to char* and int in C?
處理一些需要在函數中使用unsigned char的加密,但希望在解密后轉換為char以供使用。 所以我有:
unsigned char plaintext[16];
char *plainchar;
int plainint;
... Code now populates plaintext with data that happens to all be plain text
現在在這一點上,讓我們說明文實際上是一個“0123456789”的數據字符串。 如何將明文的值作為“012456789”獲取為plainchar,同時將plainint作為123456789?
- 編輯 -
當明文等於“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;
使plainchar等於“AAAAAAAAAA105450╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠┤∙7”,sizeof = 51.加密代碼是rijndael示例代碼,所以它應該工作正常。
謝謝,本
您的純文本字符串未終止。 所有字符串都必須有一個額外的字符,告訴字符串的結尾。 這個字符是'\\0'
。
將plaintext
變量聲明為
unsigned char plaintext[17];
完成解密后添加此項
plaintext[last_pos] = '\0';
將last_pos
更改為解密文本的最后位置,默認為16
(數組的最后一個索引)。
我認為這很簡單
plainchar = (char*)plaintext;
sscanf( plainchar, "%d", &plainint );
對於unsigned char to char *
plainchar = (char*)plaintext;
對於unsigned to int
sscanf( plainchar, "%d", &plainint );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.