Search code examples
cglib

How to convert binary to hexadecimal string in C/glib?


Is there a common way or good public domain code for converting binary (i.e. byte array or memory block) to hexadecimal string? I have a several applications that handle encryption keys and checksums and I need to use this function a lot. I written my own "quick and dirty" solution for this but it only works with binary objects of fixed size and I'm looking for something more universal. It seems pretty mundane task and I'm sure there should be some code or libraries for this. Could someone point me in the right direction, please?


Solution

  • Something like this?

    void print_hex(const char * buffer, size_t size)
    {
        for (size_t i = 0; i < size; i++)
            printf("%02x", buffer[i]);
    }