如何从十六进制转换为十进制

时间:2022-06-14 06:28:18

I apologise if this is an obvious question. I've been searching online for an answer to this and cannot find one. This isn't relevant to my code per se, it's a curiosity on my part.

如果这是一个显而易见的问题,我道歉。我一直在网上寻找答案,找不到答案。这与我的代码本身无关,这对我来说是个好奇心。

I am looking at testing my function to read start and end bytes of a buffer.

我正在测试我的函数来读取缓冲区的开始和结束字节。

If I declare a char array as:

如果我将char数组声明为:

char *buffer;
buffer = "\x0212\x03";

meaning STX12ETX - switching between hex and decimal.

意思是STX12ETX - 在十六进制和十进制之间切换。

I get the expected error:

我得到了预期的错误:

warning: hex escape sequence out of range [enabled by default]

I can test the code using all hex values:

我可以使用所有十六进制值测试代码:

"\x02\x31\x32\x03"

I am wanting to know, is there a way to escape the hex value to indicate that the following is a decimal value?

我想知道,有没有办法逃避十六进制值,以表明以下是一个十进制值?

4 个解决方案

#1


will something like this work for you ?

这样的事情对你有用吗?

char *buffer;

buffer = "\x02" "12" "\x03";

according to standard:

按标准:

§ 5.1.1.2 6. Adjacent string literal tokens are concatenated.

§5.1.1.26.相邻的字符串文字标记是连接的。

§ 6.4.4.4 3. and 7. Each octal or hexadecimal escape sequence is the longest sequence of characters that can constitute the escape sequence.

§6.4.4.43.和7.每个八进制或十六进制转义序列是可以构成转义序列的最长字符序列。

  • the escape characters:

    转义字符:

  • \' - single quote '

    \' - 单引号'

  • \" - double quote "
  • \“ - 双引号”

  • \? - question mark ?
  • \? - 问号?

  • \ - backslash \
  • \ - 反斜杠\

  • \octal digits
  • \xhexadecimal digits

So the only way to do it is concatenation of strings with the precompiler concatenation ( listing them one after another).

因此,唯一的方法是将字符串与预编译器连接串联(将它们一个接一个地列出)。

if you want to know more how the literals are constructed by compiler look at §6.4.4.4 and §6.4.5 they describe how to construct the character literals and string literals respectively.

如果你想更多地了解编译器如何构造文字,请参阅§6.4.4.4和§6.4.5,它们分别描述了如何构造字符文字和字符串文字。

#2


You can write

你可以写

"\b12"

to represent a decimal value. Altough you need to use space after hex values for it to work.

表示小数值。尽管你需要在十六进制值之后使用空格才能工作。

buffer = "\x02 \b12\x03";

Or just 12

或者只是12

buffer = "\x02 12\x03";

Basically you need to add a blank character after your hex values to indicate that it's a new value and not the same one

基本上你需要在十六进制值之后添加一个空白字符,以表明它是一个新值而不是同一个值

#3


No, there's no way to end a hexadecimal escape except by having an invalid (for the hex value) character, but then that character is of course interpreted in its own right.

不,没有办法结束十六进制转义,除非有一个无效(对于十六进制值)字符,但那个字符当然是自己解释的。

The C11 draft says (in 6.4.4.4 14):

C11草案说(见6.4.4.4 14):

[...] a hexadecimal escape sequence is terminated only by a non-hexadecimal character.

[...]十六进制转义序列仅由非十六进制字符终止。

Octal escapes don't have this problem, they are limited to three octal digits.

八进制转义没有这个问题,它们仅限于三个八进制数字。

#4


You can always use the octal format. Octal code is always 3 digits. So to get the character '<-' you simple type \215

您始终可以使用八进制格式。八进制代码始终为3位数。所以要获得字符'< - '你简单的类型\ 215

#1


will something like this work for you ?

这样的事情对你有用吗?

char *buffer;

buffer = "\x02" "12" "\x03";

according to standard:

按标准:

§ 5.1.1.2 6. Adjacent string literal tokens are concatenated.

§5.1.1.26.相邻的字符串文字标记是连接的。

§ 6.4.4.4 3. and 7. Each octal or hexadecimal escape sequence is the longest sequence of characters that can constitute the escape sequence.

§6.4.4.43.和7.每个八进制或十六进制转义序列是可以构成转义序列的最长字符序列。

  • the escape characters:

    转义字符:

  • \' - single quote '

    \' - 单引号'

  • \" - double quote "
  • \“ - 双引号”

  • \? - question mark ?
  • \? - 问号?

  • \ - backslash \
  • \ - 反斜杠\

  • \octal digits
  • \xhexadecimal digits

So the only way to do it is concatenation of strings with the precompiler concatenation ( listing them one after another).

因此,唯一的方法是将字符串与预编译器连接串联(将它们一个接一个地列出)。

if you want to know more how the literals are constructed by compiler look at §6.4.4.4 and §6.4.5 they describe how to construct the character literals and string literals respectively.

如果你想更多地了解编译器如何构造文字,请参阅§6.4.4.4和§6.4.5,它们分别描述了如何构造字符文字和字符串文字。

#2


You can write

你可以写

"\b12"

to represent a decimal value. Altough you need to use space after hex values for it to work.

表示小数值。尽管你需要在十六进制值之后使用空格才能工作。

buffer = "\x02 \b12\x03";

Or just 12

或者只是12

buffer = "\x02 12\x03";

Basically you need to add a blank character after your hex values to indicate that it's a new value and not the same one

基本上你需要在十六进制值之后添加一个空白字符,以表明它是一个新值而不是同一个值

#3


No, there's no way to end a hexadecimal escape except by having an invalid (for the hex value) character, but then that character is of course interpreted in its own right.

不,没有办法结束十六进制转义,除非有一个无效(对于十六进制值)字符,但那个字符当然是自己解释的。

The C11 draft says (in 6.4.4.4 14):

C11草案说(见6.4.4.4 14):

[...] a hexadecimal escape sequence is terminated only by a non-hexadecimal character.

[...]十六进制转义序列仅由非十六进制字符终止。

Octal escapes don't have this problem, they are limited to three octal digits.

八进制转义没有这个问题,它们仅限于三个八进制数字。

#4


You can always use the octal format. Octal code is always 3 digits. So to get the character '<-' you simple type \215

您始终可以使用八进制格式。八进制代码始终为3位数。所以要获得字符'< - '你简单的类型\ 215