显示unsigned char的第一个字符*

dar*_*eir 1 c++ printf unsigned-char

我有以下代码(我删除了无用的部分):

unsigned char* decrypted= (unsigned char *) malloc(500);
bufSize = operations.RSADecrypt(newEncrypted, bufSize, key, decrypted);
printf("Test: %s", decrypted);
Run Code Online (Sandbox Code Playgroud)

我想只显示第bufSize一个字符,decrypted因为它实际上显示了很多无意义的字符!

hmj*_*mjd 7

您可以使用"%.*s"格式说明符:

printf("Test: %.*s", bufSize, decrypted);
Run Code Online (Sandbox Code Playgroud)

指示printf()从中写出第一个bufSize字符decrypted.