简体   繁体   English

如何将无符号字符转换为字符串

[英]How to convert an unsigned char to string

I am working with the Mbed TLS Library on an ESP32 board and a function i am using takes only unsigned char and also gives unsigned char as the output我在 ESP32 板上使用Mbed TLS库,我正在使用的 function 仅采用无符号字符,并且还提供无符号字符作为 output

  mbedtls_aes_setkey_enc( &aes, key, 256 );
  mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, 48, iv, input, output );

I now need to display output using this method我现在需要使用此方法显示 output

Heltec.display->drawString(0, 0, String(output));

but here it only takes strings type... how do i go about converting the unsigned char to string here?但在这里它只需要字符串类型...我 go 如何在这里将无符号字符转换为字符串? or is there a better way to display the output (I am using a Heltec LoRa32 board with a 0.96-inch display)还是有更好的方式来显示 output(我使用的是带有 0.96 英寸显示屏的 Heltec LoRa32 板)

here is the full code这是完整的代码

#include "mbedtls/aes.h"
#include "heltec.h"

mbedtls_aes_context aes;

unsigned char key[32];
unsigned char iv[16];

unsigned char input [128];
unsigned char output[128];

size_t input_len = 40;
size_t output_len = 0;

void setup() {
  Heltec.display->init();
  Heltec.display->flipScreenVertically();
  Heltec.display->setFont(ArialMT_Plain_10);
  delay(1500);
  Heltec.display->clear();

  Heltec.display->drawString(0, 0, "Heltec.LoRa Initial success!");
  Heltec.display->display();
  delay(1000);

  mbedtls_aes_setkey_enc( &aes, key, 256 );
  mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, 48, iv, input, output );
  }

void loop() {
  Heltec.display->clear();
  Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
  Heltec.display->setFont(ArialMT_Plain_10);
  Heltec.display->drawString(0, 0, String(output);
  Heltec.display->display();
}

mbedtls_aes_crypt_cbc has 'strings' (unsigned char arrays) as input and output, not a single character: mbedtls_aes_crypt_cbc 具有“字符串”(无符号字符数组)作为输入和 output,而不是单个字符:

mbedtls_aes_crypt_cbc (mbedtls_aes_context *ctx, int mode, size_t length, unsigned char iv[16], const unsigned char *input, unsigned char *output)

drawString takes 'string' (signed char array) as input: drawString 将“字符串”(带符号的字符数组)作为输入:

void U8X8::drawString(uint8_t x, uint8_t y, const char *s)

and your output variable is char array, so all is correct (or some conversion to signed char pointer might be necessary).并且您的output变量是 char 数组,所以一切都是正确的(或者可能需要对有符号 char 指针进行一些转换)。

You forgot closing parenthesis:您忘记了右括号:

Heltec.display->drawString(0, 0, String(output));

of course if 'String' is a conversion:)当然如果'String'是一个转换:)

should be enough to:应该足以:

Heltec.display->drawString(0, 0, (char*)(output));

Good comment is that from Remy Lebeau - output is a scrambled stream of bytes, there is no sense to print them (better display as HEX instead).好的评论是来自 Remy Lebeau - output 是字节的 stream,打印它们没有意义(最好显示为 HEX)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM