how to use hex escape char with string in C? | Unix Linux Forums | Programming

  Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

how to use hex escape char with string in C?

Programming


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-27-2010
vistastar's Avatar
vistastar vistastar is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 10 June 2014, 3:17 AM EDT
Location: Galaxy
Posts: 180
Thanks: 7
Thanked 8 Times in 8 Posts
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 }
  6

gcc 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  
Old 11-27-2010
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 28 August 2014, 4:23 PM EDT
Location: Saskatchewan
Posts: 19,269
Thanks: 774
Thanked 3,236 Times in 3,034 Posts
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  
Old 11-27-2010
vistastar's Avatar
vistastar vistastar is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 10 June 2014, 3:17 AM EDT
Location: Galaxy
Posts: 180
Thanks: 7
Thanked 8 Times in 8 Posts
Why don't the complier just translates the fixed few of characters(eg: two char) after \x to an escape char?
    #4  
Old 11-29-2010
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 28 August 2014, 4:23 PM EDT
Location: Saskatchewan
Posts: 19,269
Thanks: 774
Thanked 3,236 Times in 3,034 Posts
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  
Old 11-29-2010
achenle achenle is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 28 August 2014, 8:42 PM EDT
Posts: 663
Thanks: 1
Thanked 91 Times in 87 Posts
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  
Old 11-30-2010
niterobin's Avatar
niterobin niterobin is offline
Registered User
 
Join Date: Oct 2006
Last Activity: 4 December 2013, 4:42 PM EST
Location: Stevenage, UK.
Posts: 15
Thanks: 16
Thanked 0 Times in 0 Posts
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 }
  6

with 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  
Old 11-30-2010
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 28 August 2014, 5:04 PM EDT
Location: NM
Posts: 10,180
Thanks: 275
Thanked 785 Times in 734 Posts
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
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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



All times are GMT -4. The time now is 08:01 AM.