你如何转换缓冲(字节数组),以十六进制字符串用C? [英] How do you convert buffer (byte array) to hex string in C?

查看:115
本文介绍了你如何转换缓冲(字节数组),以十六进制字符串用C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道类似的问题已经被问JAVA,C#,C ++等,但我不能找到一种方法,一个字节数组转换在C十六进制字符串。

我有:

  UINT8 BUF [] = {0,1,10,11};

我要的字节数组转换为字符串,这样我可以用printf打印字符串:

 的printf(%S \\ n,STR);

和get(冒号是没有必要的):

 00:01:0A:0B

任何帮助将大大AP preciated。

解决方案:
感谢大家的帮助。

我有一个解决办法:

 字符BUF [] = {0,1,10,11};
INT I;
字符* buf_str =(字符*)malloc的(2 *大小+ 1);
字符* buf_ptr = buf_str;
对于(i = 0; I<大小;我++)
{
    buf_ptr + = sprintf的(buf_ptr,%02X,BUF [I]);
}
sprintf的(buf_ptr,\\ n);
*(buf_ptr + 1)='\\ 0';
的printf(%S \\ n,buf_str);


解决方案

 的printf(%02X:%02X:%02X:%02X,BUF [0],BUF [1 ],BUF [2],buf中[3]);

一个更通用的方法:

  INT I;
对于(i = 0; I< X,我++)
{
    如果(I 0)的printf(:);
    的printf(%02X,BUF [I]);
}
的printf(\\ n);

来连接为一个字符串,还有你可以做这几个方面......我会一个指针可能保持到字符串的结尾,并用sprintf。你也应该保持数组大小的轨道,以确保它不得到比分配的空间较大:

  INT I;
字符* BUF2 =的StringBuf;
字符* endofbuf = +的StringBuf的sizeof(的StringBuf);
对于(i = 0; I< X,我++)
{
    / *我用5在这里,因为我们要在添加最多
       3个字符,需要后面加上一个空格'\\ n'以及必要
       一个空终止* /
    如果(BUF2 + 5℃endofbuf)
    {
        如果(I 0)
        {
            BUF2 + = sprintf的(BUF2:);
        }
        BUF2 + = sprintf的(BUF2,%02X,BUF [I]);
    }
}
BUF2 + = sprintf的(BUF2,\\ n);

I know similar questions have been asked for java, c#, c++, etc, but I couldn't find a way to convert a byte array to a hexidecimal string in C.

I have:

uint8 buf[] = {0, 1, 10, 11};

I want to convert the byte array to a string such that I can print the string using printf:

printf("%s\n", str);

and get (the colons aren't necessary):

"00:01:0A:0B"

Any help would be greatly appreciated.

SOLUTION: Thanks to all of you for helping.

I have a solution:

char buf[] = {0,1,10,11};
int i;
char* buf_str = (char*) malloc (2*size + 1);
char* buf_ptr = buf_str;
for (i = 0; i < size; i++)
{
    buf_ptr += sprintf(buf_ptr, "%02X", buf[i]);
}
sprintf(buf_ptr,"\n");
*(buf_ptr + 1) = '\0';
printf("%s\n", buf_str);

解决方案

printf("%02X:%02X:%02X:%02X", buf[0], buf[1], buf[2], buf[3]);

for a more generic way:

int i;
for (i = 0; i < x; i++)
{
    if (i > 0) printf(":");
    printf("%02X", buf[i]);
}
printf("\n");

to concatenate to a string, there are a few ways you can do this... i'd probably keep a pointer to the end of the string and use sprintf. you should also keep track of the size of the array to make sure it doesnt get larger than the space allocated:

int i;
char* buf2 = stringbuf;
char* endofbuf = stringbuf + sizeof(stringbuf);
for (i = 0; i < x; i++)
{
    /* i use 5 here since we are going to add at most 
       3 chars, need a space for the end '\n' and need
       a null terminator */
    if (buf2 + 5 < endofbuf)
    {
        if (i > 0)
        {
            buf2 += sprintf(buf2, ":");
        }
        buf2 += sprintf(buf2, "%02X", buf[i]);
    }
}
buf2 += sprintf(buf2, "\n");

这篇关于你如何转换缓冲(字节数组),以十六进制字符串用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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