Thread: Storing the output of printf into a string

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    139

    Storing the output of printf into a string

    Hello

    I have a procedure that gets an md5sum result and prints it out to console. How can I get the results into a string?


    This is what I am looking for
    Code:
    
    string get_md5_sum(unsigned char* md) {
        string retval = "";
        int i;
        for(i=0; i <MD5_DIGEST_LENGTH; i++) {
                printf("%02x",md[i]);
                retval += md[i];         ////// this is not right because I have to convert it via %02x,  how do i do this?
        }
    
        return retval;
    }


    This is my full code

    Code:
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <sys/mman.h>
    #include <iostream>
    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string>
    #include <openssl/md5.h>
    
    
    using namespace std;
    
    
    unsigned char result[MD5_DIGEST_LENGTH];
    
    
    void print_md5_sum(unsigned char* md) {
        int i;
        for(i=0; i <MD5_DIGEST_LENGTH; i++) {
                printf("%02x",md[i]);
        }
    }
    
    
    void get_md5_sum(unsigned char* md) {
        int i;
        for(i=0; i <MD5_DIGEST_LENGTH; i++) {
                printf("%02x",md[i]);
        }
    }
    
    
    // Get the size of the file by its file descriptor
    unsigned long get_size_by_fd(int fd) {
        struct stat statbuf;
        if(fstat(fd, &statbuf) < 0) exit(-1);
        return statbuf.st_size;
    }
    
    
    int main()
    {
        int file_descript;
        unsigned long file_size;
        char* file_buffer;
    
    
        string sFilePath = "myfilepath";
    
    
        file_descript = open(sFilePath.c_str(), O_RDONLY);
        if(file_descript < 0) exit(-1);
    
    
        file_size = get_size_by_fd(file_descript);
        printf("file size:\t%lu\n", file_size);
    
    
        MD5((unsigned char*) file_buffer, file_size, result);
        print_md5_sum(result);
    
    
        string sIn;
        cin >> sIn;
    
    
        return 0;
    }
    Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,485
    Perhaps sprintf() is what you need.

    But if this is really C++, then look at std::stringstream
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    13
    Then you can use the std::hex manipulator on the stream too.

  4. #4
    Registered User
    Join Date
    Jan 2014
    Posts
    139
    Thank you for the replys, I dont know if it is the lack of sleep or what but it still prints garbage

    Code:
    string get_md5_sum(unsigned char* md)
    {
        stringstream ss;
        for(int i = 0; i <MD5_DIGEST_LENGTH; i++)
        {
            printf("%02x",md[i]);
            ss << std::hex << md[i];
        }
        return ss.str();
    }

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <sstream>
    
    const int MD5_DIGEST_LENGTH = 10;
    
    std::string get_md5_sum(unsigned char* md) {
        std::stringstream ss;
        for(int i = 0; i < MD5_DIGEST_LENGTH; i++)
            ss << std::setw(2) << std::setfill('0') << std::hex
               << static_cast<unsigned>(md[i]);
        return ss.str();
    }
    
    int main() {
        unsigned char s[] = "\x01\x02\x11\x12\x88\x99\xaa\xbb\xcc\xdd";
        std::cout << get_md5_sum(s) << '\n';
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printf style output for C++
    By Mozza314 in forum C++ Programming
    Replies: 2
    Last Post: 11-09-2011, 12:31 AM
  2. Storing the output in a file
    By Sjvlsdsd in forum C Programming
    Replies: 1
    Last Post: 08-05-2011, 01:06 AM
  3. Formatting output with printf
    By Sn0wcra5h in forum C Programming
    Replies: 1
    Last Post: 01-25-2010, 03:44 PM
  4. printf output
    By shyguy24x7 in forum C Programming
    Replies: 3
    Last Post: 01-31-2009, 02:01 PM
  5. storing printf statement in to string
    By ameet in forum C Programming
    Replies: 1
    Last Post: 09-20-2004, 10:33 PM

Tags for this Thread