Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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! :)
Posted

\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[^]
 
Share this answer
 
Comments
Ivan Ivanov 83 23-Oct-12 10:12am    
So I have to use '\x...' to represent those characters as hexedecimal and '\0...' to represent them as OCTAL numbers, that's why 8 and 9 don't work :D

Thanks alot! :)
Ivan Ivanov 83 23-Oct-12 10:22am    
Strange. In the link you provided points out that the octal notation is represented by '\o...' but if it's for a Single character '\0...' also works as octal representation. And yes in the second example I gave

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

when it reaches \008 it generates a terminating character 0 and then ASCII '8'

so you're right! :)
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'.
 
Share this answer
 
Comments
Ivan Ivanov 83 23-Oct-12 10:27am    
Now as you pointed this out I saw that in the old program I have that string does not contain '\008' and '\009' :D

Thank you very much! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900