为什么字符'\ 008'不能解析为二进制8而是可以解析为ASCII 8? [英] Why characte '\008' doesn't resolve into binary 8 but into ASCII 8?

查看:114
本文介绍了为什么字符'\ 008'不能解析为二进制8而是可以解析为ASCII 8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:)

这可能不是一个很聪明的问题,:)我觉得我应该知道答案,但我不知道. :D

我的问题是我必须在字符串中添加不可打印的字符...我知道可以通过将字符的ASCII码放入"\ ..."来实现(这就是所谓的转义序列吗?) .

我在程序中编写了一个包含这样的字符的测试字符串,以查看其工作方式,但令我惊讶的是,字符"\ 008"和"\ 009"不会变成二进制8 ad 9,而变成了ASCII" 8''和``9''?!

这是字符串:

unsigned char str[] = {''\001'', ''\002'', ''\003'', ''\004'', ''\005'', ''\006'', ''\007'', ''\008'', ''\009'', ''\010'' };

// or

unsigned char str[] = "\001\002\003\004\005\006\007\008\009\010";

// I tried with single leading zero too...


同样,字符"\ 010"解析为二进制8而不是二进制10,并且每次这样的包含8或9的代码再次使ASCII 8或9而不是我想要的代码,例如:
''\ 018''=``8''代替二进制18
''\ 028''=``8''
等等.

我有一个由其他人编写的旧程序,该程序包含一个字符串,其中所有前32个不可打印的字符都以此方式显示,一切正常吗?!

显然,在每次出现8和9之后,随后的代码都会减少2,因此不会丢失任何代码,我的意思是"\ 010" =二进制8; ''\ 020''=二进制18等...所以我可以(毕竟)做我想做的事,但是为什么它必须像这样令人困惑呢?可以解决吗?

提前致谢! :)

解决方案

\ 0是null字符的转义序列,因此除非您想要null,否则不要这样做. 转义序列使用十六进制数字,即\ x41是``A''的转义序列.

我认为您可以使用正确的转义序列来初始化字符串的两种方法都可以使用.


http://msdn.microsoft.com/en-us/library/h21280bw (v = vs.80).aspx [

Hi All :)

This is probably not a very smart question, :) I feel like I''m supposed to know the answer but I don''t. :D

My problem is that I have to add non-printable characters into a string... I know that could be done through putting the ASCII number of the character into ''\...'' (Was that called an escape sequence?).

I wrote a test string in my program that contains such characters to see how it would work and I was surprised that characters ''\008'' and ''\009'' don''t become binary 8 ad 9 but ASCII ''8'' and ''9''?!

This is the string:

unsigned char str[] = {''\001'', ''\002'', ''\003'', ''\004'', ''\005'', ''\006'', ''\007'', ''\008'', ''\009'', ''\010'' };

// or

unsigned char str[] = "\001\002\003\004\005\006\007\008\009\010";

// I tried with single leading zero too...


Also the character ''\010'' is resolving into binary 8 instead of binary 10, and every time such a code that contains 8 or 9 it again makes ASCII 8 or 9 instead of the code I want, for example:
''\018'' = ''8'' sinstead of binary 18
''\028'' = ''8''
so on.

I have an old program written by someone else which ontains a string with all the first 32 non-printable characters presented this way and there everything is OK?!

Obviously after every occurance of 8 and 9 the subsequent codes are decreased with 2 so no code is lost, I mean ''\010'' = binary 8; ''\020'' = binary 18 etc... so I can (after all) do what I want, but why it has to be confusing like this? And can it be fixed?

Thanks in advance! :)

解决方案

\0 is the escape sequence for a null character so dont do that unless null is what you want.
Escape sequences use hexedecimal numbers i.e. \x41 is the escape sequence for ''A''.

I think either of the methods you use to initialise the string will work, with the correct escape sequence.


http://msdn.microsoft.com/en-us/library/h21280bw(v=vs.80).aspx[^]


Character constants in this form ''\000'' are interpreted as octal numbers. Hence octal ''\010'' is decimal 8. If you put an 8 or 9 in the constant it is an invalid octal number, so the sequence is terminated and interpreted as ''8'' or ''9''.


这篇关于为什么字符'\ 008'不能解析为二进制8而是可以解析为ASCII 8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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