如何分配一个值,使用16进制一个char *? [英] How to assign a value to a char* using hex notation?

查看:145
本文介绍了如何分配一个值,使用16进制一个char *?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常使用指针以下列方式

I usually use pointers in the following manner


    char *ptr = malloc( sizeof(char) * 100 );
    memset( ptr, 0, 100 ) ;
    strncpy( ptr, "cat" , 100 - 1 );

但是,这一次,而不是使用猫,我想用它对应的ASCII码十六进制。

But this time instead of using "cat", I want to use it ASCII equivalent in hex.

猫= 0x63,0x61,0x74,0×00

cat = 0x63, 0x61, 0x74, 0x00

我试过


    strncpy( ptr, "0x630x61" , 100 - 1 );

但它没有按照预期。

But it fails as expected.

什么是正确的语法?

我需要把0×00吗?有那么一刻,您忘了 memset的,现在我需要把0×00?由于猫的符号,一个空自动放置。

Do I need to put a 0x00 too? For a moment lets forget about memset, now do I need to put a 0x00? Because in "cat" notation, a null is automatically placed.

问候

推荐答案

\\ XXX 是十六进制格式插入字符的语法。所以你会是:

\xXX is the syntax for inserting characters in hex format. so yours would be:

strncpy( ptr, "\x63\x61\x74", 100 - 1);

您不需要把一个 \\ X00 由于具有自动报价空分隔字符串。

You don't need to put in a \x00 since having quotes automatically null-delimits the string.

这篇关于如何分配一个值,使用16进制一个char *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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