如何二进制转换为C /巧舌如簧的十六进制字符串? [英] How to convert binary to hexadecimal string in C/glib?

查看:126
本文介绍了如何二进制转换为C /巧舌如簧的十六进制字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有二进制文件(即字节数组或内存块)转换为十六进制字符串的常用方法还是不错的公共领域code?我有一个处理加密密钥和校验和多个应用程序,我需要使用此功能很多。我写我自己的快速和肮脏的解决方案,这只是到固定大小的二进制对象的作品,我期待更多的东西普遍。看来pretty平凡的任务,我相信应该有一些code或库这一点。可能有人点我在正确的方向吗?

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?

推荐答案

这样呢?

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

这篇关于如何二进制转换为C /巧舌如簧的十六进制字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆