|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
how to use hex escape char with string in C?
I want it to ouput "abcd", but it dosen't. Code:
1 #include<stdio.h>
2 int main()
3 {
4 printf("a\x62cd");
5 }
6gcc alarm.c -o alarm alarm.c: In function 'main': alarm.c:4:9: warning: hex escape sequence out of range It seems that the complier joint "cd" as part of \x62. Maybe I can write it like this: Code:
printf("a\x62""cd");but it is a bit urgly... Last edited by vistastar; 11-27-2010 at 12:28 AM.. |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
That's what I ended up having to do before. That, or seperating it out of the string entirely as in printf("a%ccd", 0x62);
|
| Sponsored Links | ||
|
|
#3
|
||||
|
||||
|
Why don't the complier just translates the fixed few of characters(eg: two char) after \x to an escape char?
|
|
#4
|
|||
|
|||
|
Why should it? There are systems where "char" isn't 8 bits... Or were, decades ago, and the C standard people latch onto this technicality like a bear trap and refuse to let go.
It plainly doesn't assume it stops at 8 bits, anyway, so the question's kind of pointless. |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
Unicode characters aren't 8 bits, and they've been spotted in use somewhat more recently than decades ago.
The only way to produce a deterministic result when the number of output bits can not be constrained is with a maximal-munch parser. |
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
I tried your code with the same result as you. Then I just tried the following code: Code:
1 #include<stdio.h>
2 int main()
3 {
4 printf("a\x62mn");
5 }
6with the output: Code:
#rob@fred:~/programs$./unixcom.exee abmn rob@fred:~/programs$ I think what is happening with your code is that the compiler is reading the \x62cd as one hex number - \xbcd. I would guess that Corona688's suggestion - printf("a%ccd", 0x62) - is your best bet. HTH, Rob. |
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
achenle - unicode or utf-16 or whatever else - have been around for a long time. They are not considered 'char' in C, they are wchar, wide characters, a different datatype. This datatype affects file orientation, which is the primary way reads & writes occur on tyy/file/device(s). wchar are NOT char, by definition and practice
try man wchar And there still are embedded systems with 32 bit char. |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Splitting URL request string including escape characters | john2022 | Shell Programming and Scripting | 4 | 09-04-2009 10:19 AM |
| Remove escape characters from string | gio001 | UNIX for Advanced & Expert Users | 2 | 07-11-2009 08:10 AM |
| sed escape char | fed.linuxgossip | Shell Programming and Scripting | 2 | 10-04-2008 06:41 AM |
| How to escape * in string when using eval | Chandu2u | Shell Programming and Scripting | 3 | 05-31-2007 04:18 AM |
| Escape Char for double quote | navik_pathak | Shell Programming and Scripting | 1 | 01-05-2007 01:34 PM |
|
|