在C中从字节转换为ASCII [英] Conversion from Byte to ASCII in C

查看:84
本文介绍了在C中从字节转换为ASCII的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以建议在C语言中将字节数组转换为ASCII的方法吗?还是将字节数组转换为十六进制,然后转换为ASCII?

Can anyone suggest means of converting a byte array to ASCII in C? Or converting byte array to hex and then to ASCII?

[04/02] [Edited]:改写一下,我希望将字节转换为十六进制并存储转换后的字节数据结构中的十六进制值。

[04/02]: To rephrase it, I wish to convert bytes to hex and store the converted hex values in a data structure. How should go about it?

致谢,
darkie

Regards, darkie

推荐答案

首先,您应该在提出问题时多加注意。很难说出您真正想听的内容。我认为您有一些二进制Blob,并希望以人类可读的形式显示它,例如将其转储到屏幕上进行调试。 (我知道我在这里可能会误解你的意思。)

First of all you should take some more care on the formulation of your questions. It is hard to say what you really want to hear. I think you have some binary blob and want it in a human readable form, e.g. to dump it on the screen for debugging. (I know I'm probably misinterpreting you here).

您可以使用 snprintf(buf,sizeof(buf),%.2x ,例如byte_array [i])可以将单个字节转换为十六进制ASCII表示形式。以下是在屏幕上转储整个内存区域的功能:

You can use snprintf(buf, sizeof(buf), "%.2x", byte_array[i]) for example to convert a single byte in to the hexadecimal ASCII representation. Here is a function to dump a whole memory region on the screen:


void
hexdump(const void *data, int size)
{
    const unsigned char *byte = data;

    while (size > 0)
    {
        size--;
        printf("%.2x ", *byte);
        byte++;
    }
}

这篇关于在C中从字节转换为ASCII的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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