简体   繁体   中英

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

  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

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? or is there a better way to display the output (I am using a Heltec LoRa32 board with a 0.96-inch display)

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 (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:

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).

You forgot closing parenthesis:

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

of course if 'String' is a conversion:)

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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