如何正确地将十六进制转义符添加到字符串字面量中? [英] How to properly add hex escapes into a string-literal?

查看:72
本文介绍了如何正确地将十六进制转义符添加到字符串字面量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C中有字符串时,可以在其中添加直接十六进制代码。

  char str [] = abcde ; //'a','b','c','d','e',0x00 
char str2 [] = abc\x12\x34; //'a','b','c',0x12、0x34、0x00

两个示例在内存中有6个字节。现在,如果您想在十六进制输入后添加值 [a-fA-F0-9] ,就会出现问题。

  //我想要:'a','b','c',0x12,'e',0x00 
//错误,十六进制太大,因为最后一个e是视为十六进制的一部分,因此变成0x12e
char问题[] = abc\x12e;

可能的解决方案是在定义后替换。

  //这将起作用,不好的主意
char解决方案[6] = abcde;
solution [3] = 0x12;

这可以工作,但是如果将其作为 const会失败。

  //这不起作用
const char解决方案[6] = abcde;
solution [3] = 0x12; //编译错误!

如何在<$之后正确插入 e c $ c> \x12 而不会触发错误?






为什么要问?当您要构建 UTF-8 字符串作为常量时,如果字符的十六进制值大于ASCII表可以容纳的范围,则必须使用该值的十六进制。

解决方案

使用3个八进制数字:

 字符问题[] = abc\ 022e; 

或拆分字符串:

  char问题[] = abc\x12 e; 






为什么这些工作:




  • 与十六进制转义符不同,标准将3位数字定义为八进制转义符的最大数量。


    6.4.4.4字符常量



    ...

     八进制转义序列:
    \八位数字
    \八位数字八位数字
    \八位数字八位数字八位数字

    ...

     十六进制转义序列:
    \x十六进制数字
    十六进制转义序列十六进制



  • 字符串文字串联被定义为比文字转义字符转换晚的翻译阶段


    5.1.1.2翻译阶段



    ...


    1. 每个源字符集成员和字符常量中的转义序列以及
      字符串文字将转换为相应的成员设置了执行字符
      的字符;如果没有相应的成员,则将其转换为实现-
      定义的成员,而不是空(宽)字符。 8)


    2. 相邻的字符串文字标记是串联在一起的。





When you have string in C, you can add direct hex code inside.

char str[] = "abcde"; // 'a', 'b', 'c', 'd', 'e', 0x00
char str2[] = "abc\x12\x34"; // 'a', 'b', 'c', 0x12, 0x34, 0x00

Both examples have 6 bytes in memory. Now the problem exists if you want to add value [a-fA-F0-9] after hex entry.

//I want: 'a', 'b', 'c', 0x12, 'e', 0x00
//Error, hex is too big because last e is treated as part of hex thus becoming 0x12e
char problem[] = "abc\x12e";

Possible solution is to replace after definition.

//This will work, bad idea
char solution[6] = "abcde";
solution[3] = 0x12;

This can work, but it will fail, if you put it as const.

//This will not work
const char solution[6] = "abcde";
solution[3] = 0x12; //Compilation error!

How to properly insert e after \x12 without triggering error?


Why I'm asking? When you want to build UTF-8 string as constant, you have to use hex values of character if it is larger than ASCII table can hold.

解决方案

Use 3 octal digits:

char problem[] = "abc\022e";

or split your string:

char problem[] = "abc\x12" "e";


Why these work:

  • Unlike hex escapes, standard defines 3 digits as maximum amount for octal escape.

    6.4.4.4 Character constants

    ...

    octal-escape-sequence:
        \ octal-digit
        \ octal-digit octal-digit
        \ octal-digit octal-digit octal-digit
    

    ...

    hexadecimal-escape-sequence:
        \x hexadecimal-digit
        hexadecimal-escape-sequence hexadecimal-digit
    

  • String literal concatenation is defined as a later translation phase than literal escape character conversion.

    5.1.1.2 Translation phases

    ...

    1. Each source character set member and escape sequence in character constants and string literals is converted to the corresponding member of the execution character set; if there is no corresponding member, it is converted to an implementation- defined member other than the null (wide) character. 8)

    2. Adjacent string literal tokens are concatenated.

这篇关于如何正确地将十六进制转义符添加到字符串字面量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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