Why is my MD5 print with extra f characters?

I have a strange problem when using the at () method for std :: string. I would like to calculate the hdd hash for a given line using this library: http://sourceforge.net/projects/libmd5-rfc/files/ The hash is calculated correctly, but there is a problem with printing it humanly. Output:

af04084897ebbf299b04082d105ab724
ffffffaf040848ffffff97ffffffebffffffbf29ffffff9b04082d105affffffb724

The code:

  #include <stdio.h>
  #include<string>
  #include<iostream>

  extern "C" {
      #include "md5.h"
  }

  int main()
  {
      md5_state_t state;
      md5_byte_t digest[16];

      std::string callid("f83bc385-26da-df11-95d5-0800275903dd@pc-archdev");

      md5_init(&state);
      md5_append(&state, (const md5_byte_t*)callid.c_str(), callid.length());

      std::string callid_digest((const char*)digest, 16);

      for(int i = 0; i < 16; ++i) {
          printf("%02x", digest[i]);
      }

      printf("\n");

      for(int i = 0; i < 16; ++i) {
          const char c = callid_digest.at(i);
          printf("%02x", c);
      }

      printf("\n");
  }

Where do the f characters come from?

+3
source share
1 answer

Byte values ​​expand.

, () char , ( f , 0x7f). unsigned char :

const unsigned char c = callid_digest.at(i); // may need to cast.
+8

Source: https://habr.com/ru/post/1770587/


All Articles