str2hex

#include 
#include 

#define KEYS_LEN 16
#define DATA_LEN 9

static void printArray(uint8_t *array, int len) {
  for (int i = 0; i < len; i++) {
    printf("%02x ", array[i]);
  }
  printf("\n");
}

static void str2hex(char *str, uint8_t *hex, int len) {
  for (int i = 0; i < len; i++) {
    int number;
    sscanf(str + i * 2, "%02x", &number);
    hex[i] = number;
  }
}

int main(void) {

  uint8_t keys[KEYS_LEN] = {0};
  uint8_t data[DATA_LEN] = {0};

  str2hex("B8B34DA2D4C4D578D8494390E3DFE7A7", keys, KEYS_LEN);
  str2hex("434D41432054657374", data, DATA_LEN);

  printArray(keys, KEYS_LEN);
  printArray(data, DATA_LEN);

  return 0;
}

Reference:

  • https://github.com/Mbed-TLS/mbedtls/blob/development/programs/pkey/rsa_decrypt.c#L141

你可能感兴趣的:(我的博客,str2hex)