简体   繁体   English

字符数组中的十六进制

[英]Hex in char array

If the following is a one byte array:如果以下是一个字节数组:

char arr[] = "\xFF";

If we do the following:如果我们执行以下操作:

char arr[] = "\xFFmyrandomstringappendedafterbyte";

printing it would result in this:打印它会导致:

byteValueGoesHEREmyrandomstringappendedafterbyte

However, if I try to do the following:但是,如果我尝试执行以下操作:

char arr[] = "\xFF98";

It will result in a warning:这将导致警告:

warning: hex escape sequence out of range

It treats 98 as part of the hexcode.它将98视为十六进制代码的一部分。 However, I would it to be treated as a string (as is myrandomstringappendedafterbyte ).但是,我会将其视为字符串(就像myrandomstringappendedafterbyte )。

I would like to have this as output byteValueGoesHERE98 .我想把它作为输出byteValueGoesHERE98

How can this be achieved without a whitespace ?如何在没有空格的情况下实现这一目标? How can I denote that 98 should be treated as a string?我如何表示 98 应该被视为一个字符串?

When string literals have only whitespace (or nothing) between them, the preprocessor combines them into a single string literal, but this does not "merge" escape sequences.当字符串文字之间只有空格(或没有空格)时,预处理器将它们组合成一个字符串文字,但这不会“合并”转义序列。 So you can just write your desired string using two strings:因此,您可以使用两个字符串编写所需的字符串:

char arr[] = "\xFF" "98";

This is four bytes including the terminating '\\0' .这是四个字节,包括终止'\\0'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM